Topic: default constructor generation in CC
Author: jason@cygnus.com (Jason Merrill)
Date: Sat, 25 Feb 1995 01:43:16 GMT Raw View
>>>>> Anil A Pal <pal@xanadu.engr.sgi.com> writes:
> The *definition* is generated when needed (used), as described in ARM
> 12.8 (pg. 296 of my edition). However, the *declaration* is done
> based on whether the class requires a constructor (i.e contains a
> member or base class with a constructor, or has a virtual function).
Uh...no.
12.1 Constructors [class.ctor]
4 A default constructor for a class X is a constructor of class X that
can be called without an argument. If there is no user-declared con-
structor for class X, a default constructor is implicitly declared.
Jason
Author: michael@sparc2.prime.com (Michael Lauer)
Date: Fri, 24 Feb 1995 15:48:51 GMT Raw View
In article <3iip6g$bth@fido.asd.sgi.com> pal@sgi.com writes:
>In article <3ii3c9$740@GRAPEVINE.LCS.MIT.EDU>, mlauer@im2.lcs.mit.edu (Michael Lauer) writes:
>|>
>|> CC 4.0 + patch 170 does not generate default constructors when it should.
>|>
>|> ////////////////////////////////////////////////////////
>|> class A
>|> {
>|> };
>|>
>|> A foo()
>|> {
>|> return A();
>|> }
>
>Neither cfront nor Microsoft Visual Basic will compile this example, either.
>This is admittedly a fine legal point, but my reading of the C++ standard is
>that:
>
> class A has no constructor declared
> class A does not require a constructor to be generated (because it has
> no base class, virtual function, etc.)
> ergo, no constructor is generated
> ergo, you cannot explicitly call the constructor
>
>Maybe worth raising this issue over in comp.std.c++?
>Do you know a compiler that does accept this construct?
>
>--
>Anil A. Pal
>pal@sgi.com (415)-390-5279
It worked on Borland 4.something, but not on any others I tried. The
relevant phrase in the ARM is in section 12.1:
"Default constructors and copy constructors, however, are generated (by
the compiler) where needed."
I would read "where needed" to include "if used, for any class," but I
can see the other interpretation, especially given that that's what
cfront did.
BTW, I ran across this in STL.
Note followups.
Michael Lauer
mlauer@mechmail.cv.com
(617) 275-1800x3879
Author: pal@xanadu.engr.sgi.com (Anil A. Pal)
Date: 24 Feb 1995 20:00:51 GMT Raw View
In article <D4IH9G.LHA@cvbnet.CV.COM>, michael@sparc2.prime.com (Michael Lauer) writes:
|>
|> It worked on Borland 4.something, but not on any others I tried. The
|> relevant phrase in the ARM is in section 12.1:
|>
|> "Default constructors and copy constructors, however, are generated (by
|> the compiler) where needed."
|>
|> I would read "where needed" to include "if used, for any class," but I
|> can see the other interpretation, especially given that that's what
|> cfront did.
There's actually two issues here (warning: very legalistic).
The compiler can both *declare* and *define* a constructor if not
specified by the user.
The *definition* is generated when needed (used), as described in ARM
12.8 (pg. 296 of my edition). However, the *declaration* is done
based on whether the class requires a constructor (i.e contains a
member or base class with a constructor, or has a virtual function).
The *definition* is then generated only if the *declaration* is
generated, AND the constructor is called.
To see this, add a virtual function to the class, and now the code
compiles.
--
Anil A. Pal
pal@sgi.com (415)-390-5279