Topic: vector<T> and T::T()
Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: 1995/05/13 Raw View
In article <D8E4or.IuI@online.tmx.com.au>,
Tony Cook <tony@online.tmx.com.au> wrote:
>John Max Skaller (maxtal@Physics.usyd.edu.au) wrote:
>: In article <smeyersD84q3o.74H@netcom.com>,
>: Scott Meyers <smeyers@netcom.com> wrote:
>: >Does T have to have a default constructor if I want to use objects of type
>: >vector<T>?
>
>: Yes.
>
>Do you mean the standard requires that T needs a deafult constructor
>for vector<T> or that the HP implementation requires one?
>
>If the first, why?
Please IGNORE my reply -- I was WRONG. I'm told
now a default constructor is NOT required.
>As an aside - I see no reason why the vector<T> class would require
>a default constructor.
Right. And it doesn't. However certain usage requires
a default constructor. Its "obvious" hopefully from the prototypes.
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd,
81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
NSW 2037, AUSTRALIA Phone: 61-2-566-2189
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1995/05/11 Raw View
John Max Skaller (maxtal@Physics.usyd.edu.au) wrote:
: In article <smeyersD84q3o.74H@netcom.com>,
: Scott Meyers <smeyers@netcom.com> wrote:
: >Does T have to have a default constructor if I want to use objects of type
: >vector<T>?
: Yes.
Do you mean the standard requires that T needs a deafult constructor
for vector<T> or that the HP implementation requires one?
If the first, why?
: >The draft standard lists no such requirement (that I can find),
: I couldn't find it in the draft either.
This strikes me as a pretty big flaw - one of the main problems with
templates is that the only place that the language specifies what
the template requirements are is in the use of the types. I'd hope
the standard would do better.
It would also mean that we could write portable code (as one
implementation might have needs that another doesn't).
As an aside - I see no reason why the vector<T> class would require
a default constructor.
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.com
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1995/05/11 Raw View
Dietmar Kuehl (kuehl@uzwil) wrote:
: Tony Cook (tony@online.tmx.com.au) wrote:
: : Jak Kirman (jak@cs.brown.edu) wrote:
: : : >>>>> "Scott" == Scott Meyers <smeyers@netcom.com> writes:
: :
: : : Scott> Does T have to have a default constructor if I want to use
: : : Scott> objects of type vector<T>? The draft standard lists no such
: : : Scott> requirement (that I can find), and
: :
: : : Scott> So: is T required to have a default constructor if I want to use
: : : Scott> vector<T>? (I'm hoping the answer is no.)
: :
: : : Unfortunately, this does seem to be a requirement; the draft standard
: : : lists many functions as taking a T() as a default argument, e.g.,
: :
: : : explicit vector(size_type n, const T& value = T(), Allocator& =
: : : Allocator());
: :
: : If you call such functions with an explicit value you shouldn't need
: : a default constructor.
: :
: I think this is not true: the draft states that type checking occures
: at the point of declaration (8.3.6.5). To check the type of the
: expression 'T()' (in the above example) a default constructor is needed.
AFAIK it also says that template functions should not be
instantiated unless called - how this would fit in I'm not sure -
but if true we could eliminate the default constructor call by
giving vector<T> two constructors to do what the one is doing now:
vector<T>(size_type count, const T&value,
Allocator&=Allocator()); // no default value
vector<T>(size_type count); // equivalent with value=T()
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.com
Author: Kalyan Kolachala <kal@chromatic.com>
Date: 1995/05/11 Raw View
maxtal@Physics.usyd.edu.au (John Max Skaller) wrote:
>In article <smeyersD84q3o.74H@netcom.com>,
>Scott Meyers <smeyers@netcom.com> wrote:
>>Does T have to have a default constructor if I want to use
objects of type
>>vector<T>?
>
> Yes.
T doesn't need to have a default constructor.
This confusion arises because of the general confusion
about templates. In case of templates a member function
for a particular type is not generated till it is used.
Generally regarded as a feature that prevents detection
of bugs, this feature has its own uses as the requirements
from type T are reduced.
The vector has a default constructor which uses default
constructors but as long as the default constructor for
the vector is not required it won't be generated. For
operations "deep down" STL uses placement new and hence the
default constructor won't be used.
>
>>The draft standard lists no such requirement (that I can
find),
Because no such requirement is needed.
-Kalyan
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1995/05/09 Raw View
Jak Kirman (jak@cs.brown.edu) wrote:
: >>>>> "Tony" == Tony Cook <tony@online.tmx.com.au> writes:
: Tony> : Unfortunately, this does seem to be a requirement; the draft
: Tony> : standard lists many functions as taking a T() as a default
: Tony> : argument, e.g.,
: Tony> : explicit vector(size_type n, const T& value = T(), Allocator& =
: Tony> : Allocator());
: Tony> If you call such functions with an explicit value you shouldn't need
: Tony> a default constructor.
: The compiler I use (Sun 4.0.1) barfs on the instantiation of vector<Foo>
: if Foo does not have a default constructor. Is this a bug? Can you
: point me to a draft section I could quote to Sun? Thanks.
Borland C++4.5 does the same thing - specificly referring to the
line with the constructor declaration.
IMHO it's a bug in the specification ... and once the spec is fixed
it will be a bug in the compilers.
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.com
Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: 1995/05/10 Raw View
In article <smeyersD84q3o.74H@netcom.com>,
Scott Meyers <smeyers@netcom.com> wrote:
>Does T have to have a default constructor if I want to use objects of type
>vector<T>?
Yes.
>The draft standard lists no such requirement (that I can find),
I couldn't find it in the draft either.
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd,
81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
NSW 2037, AUSTRALIA Phone: 61-2-566-2189
Author: smeyers@netcom.com (Scott Meyers)
Date: 1995/05/05 Raw View
Does T have to have a default constructor if I want to use objects of type
vector<T>? The draft standard lists no such requirement (that I can find),
and
class X {
public:
X(int);
};
vector<X> myVec(10, X(44));
seems to fulfill the vector interface spec in the draft standard, but I
worry that deep down in the vector implementation there will be code like
this (using the parameter names in the draft spec):
T *vectorData;
vectorData = new T[n];
As far as I can tell, however, there's no reason why it couldn't be
implemented like this instead (ignoring allocator issues),
vectorData = static_cast<T*>(operator new(sizeof(T)*n));
for (int i = 0; i < n; i++)
new (vectorData[i]) T(value);
So: is T required to have a default constructor if I want to use
vector<T>? (I'm hoping the answer is no.)
Scott
Author: Kalyan Kolachala <kal@chromatic.com>
Date: 1995/05/06 Raw View
smeyers@netcom.com (Scott Meyers) wrote:
>Does T have to have a default constructor if I want to use
objects of type
>vector<T>? The draft standard lists no such requirement (that
I can find),
There is no such requirement. Deep down in the code
"placement new" is used so the default constructor
won't be required. There is a default constructor for
vectors but that won't be generated unless you use it.
- Kalyan
Author: jak@cs.brown.edu (Jak Kirman)
Date: 1995/05/07 Raw View
>>>>> "Scott" == Scott Meyers <smeyers@netcom.com> writes:
Scott> Does T have to have a default constructor if I want to use
Scott> objects of type vector<T>? The draft standard lists no such
Scott> requirement (that I can find), and
Scott> class X {
Scott> public:
Scott> X(int);
Scott> };
Scott> vector<X> myVec(10, X(44));
Scott> seems to fulfill the vector interface spec in the draft
Scott> standard, but I worry that deep down in the vector
Scott> implementation there will be code like this (using the parameter
Scott> names in the draft spec):
Scott> T *vectorData;
Scott> vectorData = new T[n];
Scott> As far as I can tell, however, there's no reason why it couldn't be
Scott> implemented like this instead (ignoring allocator issues),
Scott> vectorData = static_cast<T*>(operator new(sizeof(T)*n));
Scott> for (int i = 0; i < n; i++)
Scott> new (vectorData[i]) T(value);
Scott> So: is T required to have a default constructor if I want to use
Scott> vector<T>? (I'm hoping the answer is no.)
Unfortunately, this does seem to be a requirement; the draft standard
lists many functions as taking a T() as a default argument, e.g.,
explicit vector(size_type n, const T& value = T(), Allocator& = Allocator());
Similarly, there are references to calls to the copy constructor
and the assignment operator.
It is hard to imagine how one could implement vectors without using copy
operations :-) but as Scott points out, the default constructor really
should not be necessary, and may be quite limiting.
Are the requirements for the key or value types for containers given
anywhere? If not, I think they should be. If so, maybe they should go
in the container section :-)
Was the default-constructor requirement deliberately decided upon? If
so, why?
Jak Kirman jak@cs.brown.edu
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is a sumptuous variety about the New England weather... In the
spring I have counted one hundred and twenty-six different kinds of
weather inside of four and twenty hours.
-- Mark Twain
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1995/05/07 Raw View
Jak Kirman (jak@cs.brown.edu) wrote:
: >>>>> "Scott" == Scott Meyers <smeyers@netcom.com> writes:
: Scott> Does T have to have a default constructor if I want to use
: Scott> objects of type vector<T>? The draft standard lists no such
: Scott> requirement (that I can find), and
: Scott> So: is T required to have a default constructor if I want to use
: Scott> vector<T>? (I'm hoping the answer is no.)
: Unfortunately, this does seem to be a requirement; the draft standard
: lists many functions as taking a T() as a default argument, e.g.,
: explicit vector(size_type n, const T& value = T(), Allocator& =
: Allocator());
If you call such functions with an explicit value you shouldn't need
a default constructor.
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.com
Author: kuehl@uzwil (Dietmar Kuehl)
Date: 1995/05/08 Raw View
Tony Cook (tony@online.tmx.com.au) wrote:
: Jak Kirman (jak@cs.brown.edu) wrote:
: : >>>>> "Scott" == Scott Meyers <smeyers@netcom.com> writes:
:
: : Scott> Does T have to have a default constructor if I want to use
: : Scott> objects of type vector<T>? The draft standard lists no such
: : Scott> requirement (that I can find), and
:
: : Scott> So: is T required to have a default constructor if I want to use
: : Scott> vector<T>? (I'm hoping the answer is no.)
:
: : Unfortunately, this does seem to be a requirement; the draft standard
: : lists many functions as taking a T() as a default argument, e.g.,
:
: : explicit vector(size_type n, const T& value = T(), Allocator& =
: : Allocator());
:
: If you call such functions with an explicit value you shouldn't need
: a default constructor.
:
I think this is not true: the draft states that type checking occures
at the point of declaration (8.3.6.5). To check the type of the
expression 'T()' (in the above example) a default constructor is needed.
dk
--
http://www.informatik.uni-konstanz.de/~kuehl/ich.html
dietmar.kuehl@uni-konstanz.de
I am a realistic optimist - that's why I appear to be slightly pessimistic
Author: jak@cs.brown.edu (Jak Kirman)
Date: 1995/05/08 Raw View
>>>>> "Tony" == Tony Cook <tony@online.tmx.com.au> writes:
Tony> : Unfortunately, this does seem to be a requirement; the draft standard
Tony> : lists many functions as taking a T() as a default argument, e.g.,
Tony> : explicit vector(size_type n, const T& value = T(), Allocator& =
Tony> : Allocator());
Tony> If you call such functions with an explicit value you shouldn't need
Tony> a default constructor.
The compiler I use (Sun 4.0.1) barfs on the instantiation of vector<Foo>
if Foo does not have a default constructor. Is this a bug? Can you
point me to a draft section I could quote to Sun? Thanks.
#include <vector.h>
class Foo
{
public:
Foo (int a, int b) { cout << a << " " << b << endl; }
};
main (int argc, char *argv[])
{
Foo a (1, 2);
vector<Foo> v (3, a); // line 24
}
"vector2.C", line 24: Error: Could not find a match for Foo::Foo().
"vector2.C", line 24: Where: While specializing vector<Foo>.
"vector2.C", line 24: Where: Specialized in non-template code.
Jak Kirman jak@cs.brown.edu
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cigarettes are a drug of entry for teen-agers to the world of hard drugs.
-- Joseph Califano