 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Garrett Kajmowicz Guest
|
Posted: Tue Jan 04, 2005 1:18 pm Post subject: Spec. question: STL and default constructors |
|
|
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 ]
|
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Tue Jan 04, 2005 2:17 pm Post subject: Re: Spec. question: STL and default constructors |
|
|
Garrett Kajmowicz wrote:
| Quote: | 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);
| Quote: | 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.
| Quote: | 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.
| Quote: | 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 ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Jan 05, 2005 3:48 pm Post subject: Re: Spec. question: STL and default constructors |
|
|
Garrett Kajmowicz wrote:
| Quote: | Quick question.
If I create a std::vector<T>, is T required to have a default
constructor?
|
No.
| Quote: | 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.
| Quote: | 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.
| Quote: | 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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|