Topic: class pair<> in STL


Author: Allan Woloshin <ALLANW@MICROFRAME.COM>
Date: 1997/01/15
Raw View
[Moderator's note: This discussion is beginning to drift away from
C++ standardization.  I'm setting the Followup-to line to
comp.lang.c++.moderated.  mha]

Arvan Pritchard <arvan@uk.gdscorp.com> wrote:
>Enno Rehling wrote:

>> I'm working with the HP implementation of STL that came with my Visual
C++
>> 4.0, and have a question:
>> Why doesn't pair<> have a default constructor? The way it's
implemented,
[snip]
I don't know about the HP implementation.  But the SGI implementation
doesn't use a default constructor unless both types are
DefaultConstructible.  From http://www.sgi.com/Technology/STL/pair.html,

see this text:
    Additional operations have additional requirements. Pair's default
    constructor may only be used if both T1 and T2 are
DefaultConstructible, ...

>For the time being add
>    pair() {}
>to pair.h, or if you want to use STL maps add
>    pair() : first(T1()), second(T2()) {}
>(At least that is what I concluded I needed on VC 4.1 to make pair<const
>int,int> work).

Don't do it!  Never modify a third-party header file if you can help it.

If you can't switch to the SGI implementation, or of that simply doesn't

help you, at least remain OOP-compliant.  Remember, Real OOPers don't
MODIFY, they just DERIVE.

    typedef<class T1, class T2> class PairWDefault : public Pair<T1,T2>
{
public:
        PairWDefault() : pair(T1(),T2()) {}
        // Other functions may need overrides as well, esp. Copy-ctor,
op=(), etc.
    };

>It may be worth searching the web for a more recent copy of the HP
>implementation of the STL.
True.  Until then, check out http://www.sgi.com/Technology/STL/
Good luck.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: phalpern@truffle.ma.ultranet.com (Pablo Halpern)
Date: 1997/01/17
Raw View
"Enno Rehling" <enno@uni-paderborn.de> wrote:

>Hello,
>
>I'm working with the HP implementation of STL that came with my Visual C++
>4.0, and have a question:
>
>Why doesn't pair<> have a default constructor? The way it's implemented,
>it's impossible to write:
>
>vector< pair<int,int> > my_vector;
>

The HP reference implementation of STL is handicapped so that it will
work on almost any compiler with a minimal implementation of templates.
Many compilers will instantiate all functions of a template class as
soon as the class is instantiated. In otherwords, writing:

   pair<X,Y> var;

will cause ALL of the functions of pair<X,Y> to be instantiated. "So
what?" you ask.  Well, assume there is a default constructor in pair<>:

  pair() : first(T1()), second(T2()) { }

and imagine that class X does not have a default constuctor. Then the
declaration of var above will cause the default constructor of pair<X,Y>
to be instantiated. This default constructor tries to use the default
constructor for X, which doesn't exist. Therefore, adding a default
constructor to pair<> will cause pair<> to be unusable for any class
that doesn't have a default contructor. The fact that you had trouble
with vector indicates that your compiler still has this problem.

The problem goes away for new compilers that only instantiate a function
if that function is actually used. A full-featured version of STL for
those compilers WILL (I think) supply a default constructor for pair<>.
A full-featured version of STL will not, however, compile on most of
today's compilers.

The solution to your problem, as Allan Woloshin pointed out, is to
derive a new pair class from the existing one and add a default
constructor (as well as the normal constructor) in the derived class:

  template <class T1, class T2>
  struct pairWithDefault : public pair<T1,T2>
  {
    pairWithDefault() : pair<T1,T2>(T1(), T2()) { }
    pairWithDefault(const T1& a, const T2& b) : pair<T1,T2>(a, b) { }
    // Use compiler-generated copy constructor, assignment operator
    // and destructor
  };

A better solution would be to get a new compiler with a full-featured
version of the standard library ;-)

-------------------------------------------------------------
Pablo Halpern                   phalpern@truffle.ultranet.com

I am self-employed. Therefore, my opinions *do* represent
those of my employer.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]






Author: Kresimir Fresl <fresl@grad.hr>
Date: 1997/01/13
Raw View
Enno Rehling wrote:

> I'm working with the HP implementation of STL that came with
> my Visual C++ 4.0, and have a question:

> Why doesn't pair<> have a default constructor?

It seems that Visual C++ 4.0 has rather old version of STL.
At `ftp.cs.rpi.edu', in directory `/pub/stl' you can find two
newer versions that both have default constructor in `pair<>':

(1) In subdirectory `hp' there is file `stl.zip' dated
    Jan. 7th, 1996. In `pair.h' there is default constructor

        pair() {}

(2) In subdirectory `book' there's also file `stl.zip', but
    dated Jun 8th, 1996. `pair.h' has default constructor

        pair() : first(T1()), second(T2()) {}

    README says: ``Necessary for (at least) xlC, CSet++,
    and Microsoft Visual C++.''


Kresimir Fresl
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Arvan Pritchard <arvan@uk.gdscorp.com>
Date: 1997/01/14
Raw View
Enno Rehling wrote:
> I'm working with the HP implementation of STL that came with my Visual C++
> 4.0, and have a question:
>
> Why doesn't pair<> have a default constructor? The way it's implemented,
> it's impossible to write:
>
> vector< pair<int,int> > my_vector;
>
> because vector requires its components to have a default constructor for
> its own:
>
>     vector(size_type n, const T& value = T()) { ... }
>
I believe that eventually compilers should allow you to build
vectors of classes without default constructors, provided you
do not use this constructor ...

For the time being add

    pair() {}

to pair.h, or if you want to use STL maps add

    pair() : first(T1()), second(T2()) {}

(At least that is what I concluded I needed on VC 4.1 to make pair<const
int,int> work).

It may be worth searching the web for a more recent copy of the HP
implementation of the STL.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]






Author: "Enno Rehling" <enno@uni-paderborn.de>
Date: 1997/01/11
Raw View
Hello,

I'm working with the HP implementation of STL that came with my Visual C++
4.0, and have a question:

Why doesn't pair<> have a default constructor? The way it's implemented,
it's impossible to write:

vector< pair<int,int> > my_vector;

because vector requires its components to have a default constructor for
its own:

    vector(size_type n, const T& value = T()) { ... }

Any idea why this is so?
Enno
--

rehling@usa.net  Int+ 49 (0) 5251-760796  Paderborn, Germany
SCIENCE: A way of finding things out and then making them work. Science
explains what is happening around us the whole time. So does RELIGION, but
science is better because it comes up with more understandable excuses when
it is wrong. There is a lot more Science than you think. -- From A
Scientific Encyclopedia for the Enquiring Young Nome by Angalo de
Haberdasheri (Terry Pratchett, Wings)


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]