Topic: Vector of objects without default ctor?


Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/05/19
Raw View
Paul Hamilton wrote:
>
> In article <355B2A9B.1493@lehman.com>, cgeorges@lehman.COM wrote:
>
> > My compiler (VC++5 SP3) doesn't seem to accept a vector of objects of a
> > class without a default ctor:

> > void main()
> > {
> >     vector<C> vc;
> > }
> >
> > The error is: 'C::C' : no appropriate default constructor available
> >
> > Is this what the standard wants or is a particularity of the compiler?
> >
>
> No I think the standard needs that.

It's just a compiler limitation.

Some compilers try to instanciate all members of a template class,
causing this problem. Others have a more subtle problem (I think
that they try to check the syntax, and barf on T()), and have the
same behaviour.

Hopefully there are better compilers which hapilly compile that.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/05/19
Raw View
Paul Hamilton wrote:
>
> In article <355B2A9B.1493@lehman.com>, cgeorges@lehman.COM wrote:
>
> > My compiler (VC++5 SP3) doesn't seem to accept a vector of objects of a
> > class without a default ctor:
> >
> > class C
> > {
> > public: int att;
> >         C(int val) : att(val) {}
> > };
> >
> > void main()
> > {
> >     vector<C> vc;
> > }
> >
> > The error is: 'C::C' : no appropriate default constructor available
> >
> > Is this what the standard wants or is a particularity of the compiler?
> >
>
> No I think the standard needs that. It needs to be able to construct new
> objects on it's own for "empty" objects in the list (like if you reserve
> space).

If you don't call any of the functions which uses a default constructor,
you don't need a default constructor. Unfortunately, this is a special
case of the general rule, that templated functions which are not used
don't need to be instantiated. It is a rule that MSVC++ hasn't
implemented correctly yet.
The functions that need the default constructor are the ones that
add/create new elements in the vector, which are to filled in with
copies of a specified object, which defaults to T(). Even if you use
those functions, but never use the default argument, the default
constructor is not needed - the version that uses the default argument
is actually a different function from the one that doesn't.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/05/19
Raw View
Paul Hamilton wrote:
>
> In article <355B2A9B.1493@lehman.com>, cgeorges@lehman.COM wrote:
>
> > My compiler (VC++5 SP3) doesn't seem to accept a vector of objects of a
> > class without a default ctor:
> >
> > class C
> > {
> > public: int att;
> >         C(int val) : att(val) {}
> > };
> >
> > void main()
> > {
> >     vector<C> vc;
> > }
> >
> > The error is: 'C::C' : no appropriate default constructor available
> >
> > Is this what the standard wants or is a particularity of the compiler?
> >
>
> No I think the standard needs that. It needs to be able to construct new
> objects on it's own for "empty" objects in the list (like if you reserve
> space).
Paul Hamilton wrote:
>
> In article <355B2A9B.1493@lehman.com>, cgeorges@lehman.COM wrote:
>
> > My compiler (VC++5 SP3) doesn't seem to accept a vector of objects of a
> > class without a default ctor:
> >
> > class C
> > {
> > public: int att;
> >         C(int val) : att(val) {}
> > };
> >
> > void main()
> > {
> >     vector<C> vc;
> > }
> >
> > The error is: 'C::C' : no appropriate default constructor available
> >
> > Is this what the standard wants or is a particularity of the compiler?
> >
>
> No I think the standard needs that. It needs to be able to construct new
> objects on it's own for "empty" objects in the list (like if you reserve
> space).

It doesn't need to construct new objects on the reserved space, only
objects within the current size of the vector need to be initialized.

If you don't call any of the functions which uses a default constructor,
you don't need a default constructor. This is a special case of the
general rule, that templated functions which are not used don't need to
be instantiated. Unfortunately it is a rule that MSVC++ hasn't
implemented correctly yet.
The functions that need the default constructor are the ones that
add/create new elements in the vector, which are to filled in with
copies of a specified object, which defaults to T(). Even if you use
those functions, but never use the default argument, the default
constructor is not needed - the version that uses the default argument
is actually a different function from the one that doesn't.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/05/19
Raw View
Paul Hamilton wrote:
>
> In article <355B2A9B.1493@lehman.com>, cgeorges@lehman.COM wrote:
>
> > My compiler (VC++5 SP3) doesn't seem to accept a vector of objects of a
> > class without a default ctor:
> >
> > class C
> > {
> > public: int att;
> >         C(int val) : att(val) {}
> > };
> >
> > void main()
> > {
> >     vector<C> vc;
> > }
> >
> > The error is: 'C::C' : no appropriate default constructor available
> >
> > Is this what the standard wants or is a particularity of the compiler?
> >
>
> No I think the standard needs that. It needs to be able to construct new
> objects on it's own for "empty" objects in the list (like if you reserve
> space).

If you don't call any of the functions which uses a default constructor,
you don't need a default constructor. Unfortunately, this is a special
case of the general rule, that templated functions which are not used
don't need to be instantiated. It is a rule that MSVC++ hasn't
implemented correctly yet.
The functions that need the default constructor are the ones that
add/create new elements in the vector, which are to filled in with
copies of a specified object, which defaults to T(). Even if you use
those functions, but never use the default argument, the default
constructor is not needed - the version that uses the default argument
is actually a different function from the one that doesn't.


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: tlhol@ibm.net (Thomas Holaday)
Date: 1998/05/19
Raw View
In article <355B2A9B.1493@lehman.com>, cgeorges@lehman.COM says...
> My compiler (VC++5 SP3) doesn't seem to accept a vector of objects of a
> class without a default ctor.
> Is this what the standard wants or is a particularity of the compiler?

This is standard.  There is no syntax to let the compiler know what
arguments to use for a non-default constructor.  Here are some ways to do
it anyway:

given:
class C {
public:
  C(int);
};

void f_ten_Cs_initialized_to_42()
{

  class LocalC
  :
  public C {
    public:
 LocalC() : C(42) {}
  };

  LocalC myArray[10] ;

}

For an arbitrarily complicated thingie, use:

namespace {

  struct Arbitrary
  {
    Arbitrary() : val(0) {}
    int operator()() { return val++ ; }
    int val;
    void reset() { val = 0 ; }
  };
  Arbitrary a;
}


void f_ten_Cs_initialized_from_0_to_9()
{

  class LocalC
  :
  public C {
    public:
      LocalC() : C(a()) { }

  a.reset();
  LocalC myArray[10] ;
}

And remember, after compiling something like this, always look at the
assembler.


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Michael Tsirkin <mtsirkin@iil.intel.com>
Date: 1998/05/21
Raw View
Alexandre Oliva wrote:

> Cristian Georgescu <cgeorges@lehman.COM> writes:
>
> > My compiler (VC++5 SP3) doesn't seem to accept a vector of objects
> of a
> > class without a default ctor:
>
> It's broken, your program should be accepted.

I had this too.
SP3 breaks this.
Just go from VC++ SP3 back to SP2, and you'll be OK.
Strangely, some people have this problem and some don't.



MST
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "X. Liu" <liu@erdw.ethz.ch>
Date: 1998/05/25
Raw View
>> > My compiler (VC++5 SP3) doesn't seem to accept a vector of objects
>> of a
>> > class without a default ctor:
>>
>> It's broken, your program should be accepted.
>
>I had this too.
>SP3 breaks this.
>Just go from VC++ SP3 back to SP2, and you'll be OK.
>Strangely, some people have this problem and some don't.

I use VC++ with SP3. The vector of objects without constructor works OK!

 [Moderator's note: further discussion about VC++ should
 be directed to a newsgroup in the microsoft.* hierarchy
 or the comp.os.ms* subhierarchy. Thanks. -moderator(fjh).]

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Albrecht Reimann" <are@basilproducts.dk>
Date: 1998/05/26
Raw View
VC's STL implementation needs a default constructor. If you don't provide a
contructor at all, it creates one itself, like it would create a copy
constructor etc. As soon as you provide a constructor yourself (no matter
what arg's it takes), you will have to provide the "special" constructors
yourself.
That's my experience with c++ compilers, not an interpretation of the
language definition...

best regards/Albrecht



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/05/19
Raw View
Cristian Georgescu <cgeorges@lehman.COM> wrote:
: My compiler (VC++5 SP3) doesn't seem to accept a vector of objects of a
: class without a default ctor:

: class C
: {
: public: int att;
:         C(int val) : att(val) {}
: };

: void main()
: {
:     vector<C> vc;
: }

: The error is: 'C::C' : no appropriate default constructor available

: Is this what the standard wants or is a particularity of the compiler?

I think that the compiler is wrong.

If you'd use

vector<C> vc(10);

Then the compiler would be right. It would need to construct
10 C objects, but would not know how to do it without a default
constructor. However, it seems to me, that it does not need
a default constructor for what you do, so it has no right
to demand it.

One possible explanation might be that VC++ instantiates (I don't
know whether it does it or not) member functions for template
classes, even if they are not used. This is non-conformant. For
example, when it attempts to instantiate vector<C>::vector(size_type),
it needs the default constructor.

Hope this helps.

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: paul@mbed.com (Paul Hamilton)
Date: 1998/05/19
Raw View
In article <355B2A9B.1493@lehman.com>, cgeorges@lehman.COM wrote:

> My compiler (VC++5 SP3) doesn't seem to accept a vector of objects of a
> class without a default ctor:
>
> class C
> {
> public: int att;
>         C(int val) : att(val) {}
> };
>
> void main()
> {
>     vector<C> vc;
> }
>
> The error is: 'C::C' : no appropriate default constructor available
>
> Is this what the standard wants or is a particularity of the compiler?
>

No I think the standard needs that. It needs to be able to construct new
objects on it's own for "empty" objects in the list (like if you reserve
space).

Paul.
--
Paul Hamilton
Software Engineer
mBED Software
paul@mbed.com
http://www.mbed.com
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Cristian Georgescu <cgeorges@lehman.COM>
Date: 1998/05/14
Raw View
My compiler (VC++5 SP3) doesn't seem to accept a vector of objects of a
class without a default ctor:

class C
{
public: int att;
        C(int val) : att(val) {}
};

void main()
{
    vector<C> vc;
}

The error is: 'C::C' : no appropriate default constructor available

Is this what the standard wants or is a particularity of the compiler?

--
                      Cristian Georgescu
_________________________________________________
                      email: CGeorges@lehman.com
                      tel:   (212) 526-3502

    _/      _/_/_/_/  Lehman Brothers
   _/      _/    _/   Equity Finance Systems
  _/      _/_/_/_/    World Financial Center
 _/      _/    _/     200 Vesey Street
_/_/_/  _/_/_/_/      New York, NY 10285.
_________________________________________________



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/05/17
Raw View
Cristian Georgescu <cgeorges@lehman.COM> writes:

> My compiler (VC++5 SP3) doesn't seem to accept a vector of objects of a
> class without a default ctor:

It's broken, your program should be accepted.  VC++ is probably unable
to compile it because one of the constructors of vector<T> (which is
not the one you're using) takes (size_type, const T& = T()), so you
should provide the second argument explicitly if your type does not
have a default constructor.  However, since that constructor is not
used, it shouldn't be instantiated.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]