🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Jason Merrill Thu, 04 Jan 1996 14:16
Re: implicitly declared default constructor isn't generated
#2980
Author: 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. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads