🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2980

Re: implicitly declared default constructor isn't generated

#2980
From: Jason Merrill
Date: Thu, 04 Jan 1996 14:16
99 lines
4021 bytes
[ Moderator's note: this thread originated from gnu.g++.bug.	]
[ To fill you in on the missing context, the question at hand   ]
[ is whether or not the code fragment				]
[								]
[	struct Int {						]
[		Int( int x ) : x( x ) {}			]
[		int x;						]
[	};							]
[								]
[	int main() {						]
[		Int* pi( new Int[ 2 ] );			]
[		delete pi;					]
[		return 0;					]
[	}							]
[								]
[ is well-formed.	-fjh.					]

>>>>> Rohan LENARD <rjl@iassf.easams.com.au> writes:

> This looks like a hang-over from the ARM, which explicitely stated that
> array of objects will be created by calling all the default ctors.  The
> April draft working paper seems to allow this -

> #include <stdio.h>

> struct Int
>           {
>               Int( int x ) : x( x ) {
>    fprintf(stderr,"X = %d\n",x);
>   }
>               int x;
>           };
>           int
>           main()
>           {
>               Int* pi = new Int[ 2 ](3);	// Initialise all elements with
> ctor called with (3).
>               delete pi;
>               return 0;
>           }

Nope; that does not fall into any of the well-formed cases, since Int[2] is
not a class type.

>> Quite right, I read the draft working paper too quickly.  From my
>> point of view though, it should still compile.  According to section
>> 5.3.4 [expr.new] verse 14, the object created by the expression `new
>> Int[ 2 ]' has indeterminate value, which I took to mean the default
>> constructor isn't called, while `new Int[ 2 ]()' does invoke the
>> default constructor.  Is that not a reasonable interpretation?

Nope; Int[2] is an array of non-POD class type, so it falls under item 1
of 5.3.4p14.

>> Here's the code snippet again:

>> struct Int
>> {
>> Int( int x ) : x( x ) {}
>> int x;
>> };

>> int
>> main()
>> {
>> Int* pi( new Int[ 2 ] );
>> delete pi;

>> return 0;
>> }

14If the type of the object created by the new-expression is T:

  --If  the  new-initializer  is  omitted  and T is a non-POD class type
    (_class_) (or array thereof), then if the default constructor for  T
    is accessible it is called, otherwise the program is ill-formed;

  --If   the   new-initializer   is   omitted   and  T  is  a  POD  type
    (_basic.types_), then the  object  thus  created  has  indeterminate
    value;

  --If  the  new-initializer  is  of the form (), default-initialization
    shall be performed (_dcl.init_);

  --If the new-initializer is of the form expression-list) and  T  is  a
    class type, the appropriate constructor is called, using expression-
    list as the arguments (_dcl.init_);

  --If the new-initializer is of the form expression-list) and T  is  an
    arithmetic,  enumeration,  pointer,  or  pointer-to-member  type and
    expression-list comprises exactly one expression, then the object is
    initialized  to  the  (possibly  converted)  value of the expression
    (_dcl.init_);

  --Otherwise the new-expression is ill-formed.
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <fjh-960105-011629@cs.mu.oz.au>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 04 Jan 96 14:16:19 GMT
Approved: fjh@cs.mu.oz.au
X-Original-Date: 04 Jan 96 14:01:51 GMT
Organization: -
Newsgroups: comp.std.c++
Message-ID: <fjh-960105-011629@cs.mu.oz.au>
Cc: warnerd@pascal.dartmouth.edu, bug-g++@prep.ai.mit.edu,
        std-c++@ncar.ucar.edu, jason@cygnus.com
Subject: Re: implicitly declared default constructor isn't generated 
In-Reply-To: Your message of "Thu, 04 Jan 1996 15:06:09 +1100."
             <0kup8l3_3gX_0UIUo0@iassf.easams.com.au> 
X-Original-Date: Wed, 03 Jan 1996 21:29:57 -0800
From: Jason Merrill <jason@cygnus.com>
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMOvhXOEDnX0m9pzZAQGjuwGAiWjHFTt644SP2gUuEh6QIJyObzma8Beq
 XsuLQZ4yHBNNFcxHuHP3PgKIAS4ErnYA
 =IHLm