Topic: Spec. question: STL and default constructors


Author: garrett@garrett.dyndns.biz (Garrett Kajmowicz)
Date: Tue, 4 Jan 2005 13:18:05 GMT
Raw View
Quick question.

If I create a std::vector<T>, is T required to have a default constructor?
I know that the spec requires an assignment operator or copy constructor,
but I can't find anything about a default constructor.  Please point out
reference in standard if there isn't one.

If not, may I ask the reasoning behind not requiring default
constructors.  I can tell you that it is causing me a bit of pain (I'm
going an independent implementation of the C++ library for embedded
systems:  cxx.uclibc.org)

Thank you for the information.

- Garrett Kajmowicz

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jdennett@acm.org (James Dennett)
Date: Tue, 4 Jan 2005 14:17:22 GMT
Raw View
Garrett Kajmowicz wrote:

> Quick question.
>
> If I create a std::vector<T>, is T required to have a default constructor?

Only if no initial value is specified as a constructor
argument to std::vector<T>.  E.g. a default constructor
for T is required if I write

std::vector<T> uses_default_ctor(5);

but not if I write

T initial_value(42);
std::vector<T> copies_only(5, initial_value);

> I know that the spec requires an assignment operator or copy constructor,
> but I can't find anything about a default constructor.  Please point out
> reference in standard if there isn't one.

There is not one, so I can't point it out.

> If not, may I ask the reasoning behind not requiring default
> constructors.

It makes std::vector more generic, without any perceived serious
difficulty to implementers.

> I can tell you that it is causing me a bit of pain (I'm
> going an independent implementation of the C++ library for embedded
> systems:  cxx.uclibc.org)

Is there a reason why you can't do the normal thing of copy
constructing, from a default-constructed instance if none is
supplied?

-- James

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kanze@gabi-soft.fr
Date: Wed, 5 Jan 2005 09:48:41 CST
Raw View
Garrett Kajmowicz wrote:
> Quick question.

> If I create a std::vector<T>, is T required to have a default
> constructor?

No.

> I know that the spec requires an assignment operator or copy
> constructor, but I can't find anything about a default
> constructor.  Please point out reference in standard if there
> isn't one.

That's going to be difficult.  Pointing out the reference when
there isn't one, I mean.  And that's exactly the situation.
There is no requirement that T have a default constructor.  So
obviously, we cannot point out where this requirement doesn't
appear.

> If not, may I ask the reasoning behind not requiring default
> constructors.

Because it isn't needed, and there was no desire to impose
artificial constraints.

> I can tell you that it is causing me a bit of pain.  (I'm
> going an independent implementation of the C++ library for
> embedded systems: cxx.uclibc.org)

I'm not sure how this could be a pain.  You're allowed to
provide a default constructor for T, if you want one.  The
standard library just won't normally use it, that's all.

You do have to provide copy and assignment.  And that can be a
pain.  When it is, of course, the answer is such types don't
belong in standard containers.  (Often, in such cases, what you
really want is a container of pointers to the objects.)

--
James Kanze           GABI Software         http://www.gabi-soft.fr
Conseils en informatique orient   e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]