Topic: Can structs within classes be initialized with mem-initializer ?
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sun, 26 Jun 1994 22:22:20 GMT Raw View
In article <2ucv7o$nla@f111.iassf.easams.com.au> rjl@f111.iassf.easams.com.au (Rohan LENARD) writes:
>It seems to me that this would be a nice thing to allow, because aggregates
>can be initialised as variables like this -
>
>struct moocow {
> int A;
> double B;
>};
>moocow a = {1};
>
>Where the first element is set to one, and the compiler is required to set
>the rest of the elements to zero.
>
>I would have though a mem-initalizer should have comprable features.
But instead, the "default constructor" is called, or,
for entities without constructors nothing is done, as if:
X() : Y() {}
had been written, rather than as if
X() : Y(0) {}
had been written for Y a scalar. You can always write that explicitly.
More useful would have been:
class X {
int x = 0; // default to 0
double y = 0;
X() {} // means X() : x(0), y(0.0) {}
};
which I proposed and was rejected.
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd, CSERVE:10236.1703
6 MacKay St ASHFIELD, Mem: SA IT/9/22,SC22/WG21
NSW 2131, AUSTRALIA
Author: rjl@f111.iassf.easams.com.au (Rohan LENARD)
Date: 27 Jun 1994 09:54:43 +1000 Raw View
In article <Cs0zH9.KtK@ucc.su.oz.au>,
John Max Skaller <maxtal@physics.su.OZ.AU> wrote:
John>In article <2ucv7o$nla@f111.iassf.easams.com.au> rjl@f111.iassf.easams.com.au (Rohan LENARD) writes:
John>>It seems to me that this would be a nice thing to allow, because aggregates
John>>can be initialised as variables like this -
John>>
John>>struct moocow {
John>> int A;
John>> double B;
John>>};
John>>moocow a = {1};
John>>
John>>Where the first element is set to one, and the compiler is required to
John>>set the rest of the elements to zero.
John>>
John>>I would have though a mem-initalizer should have comprable features.
John>
John> But instead, the "default constructor" is called, or,
John>for entities without constructors nothing is done, as if:
John>
John> X() : Y() {}
John>
John>had been written, rather than as if
John>
John> X() : Y(0) {}
John>
John>had been written for Y a scalar. You can always write that explicitly.
John>More useful would have been:
John>
John> class X {
John> int x = 0; // default to 0
John> double y = 0;
John> X() {} // means X() : x(0), y(0.0) {}
John> };
John>
John>which I proposed and was rejected.
Doesn't anyone like orthogonality any more ? (Hurumphhhh !@#!@#@!)
Did anyone see the syntax change I suggested ?
Swap the expression-list_opt_ with initializer-list_opt_ in
mem-initializer-list
I think this would result in a very minor change in current compilers, but
would allow aggregates to be initialised.
An agregate is not meant to be a class and should not have to have a
constructor (because it is then no longer an aggregate). Imagine a big
aggregate (which is purely used to collect related elements together).
eg.
struct MessageData {
int Field1;
...
double Field9999;
};
If one is forced to write a constructor for this, then every element should
appear in the constructor, otherwise one relies on undefined behaviour if
you use a value without it being initialised.
If you decide that your aggregate is going to be a global variable then you
can simply go (and all elements are automatically initialised to 0) -
MessageData TheData = {0};
But if you do the right thing and decide not to spoil the global name-space
so you have you data as an attribute in an object which should contain the
data, then you are forced to write 10000 lines of code to do something which
the compiler is good at doing.
Stupidity is ripe :-)
Thank-goodness for the GNU compiler (at least GNU C++ will allow thi sometime
in the future).
Regards,
Rohan
--
------------------------------------------------------------------------
rjl@iassf.easams.com.au |
Rohan Lenard | .sig on holiday
+61-2-367-4555 |
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Thu, 23 Jun 1994 06:12:47 GMT Raw View
In article <2u8j2f$li6@f111.iassf.easams.com.au> rjl@f111.iassf.easams.com.au (Rohan LENARD) writes:
>Hi folks,
>
>Compare the initialisation of a struct within and out of a class -
>
>
>// Struct outside of class
>struct Thing {
> double a;
> int b;
>};
>
>Thing blob = { 10.0, -1 };
>
>// Struct inside of class ?
>class Foo {
>public:
> Foo() : ClassBlob({10.0, -1}) {};
> // or should it be
> // Foo() : ClassBlob({double(10.0),int(-1)}) {};
> // or could it event be
> // Foo() : ClassBlob(blob) {};
> ~Foo();
>private:
> Thing ClassBlob;
>};
>
>Is it possible to initialise ClassBlob like I have above, or not.
>
>The ARM seems to suggest that it is possible, but then both compilers I tried
>say it isn't. What are people thoughts ?
>
>1. Section 8.4.1. Aggregates - basically says that Thing is an aggregate, and
> thus can be initialised with an initialiser list starting and finishing
> with matching braces. (But it's still an object)
>
>2. Section 12.6 Initialization - also agrees that an object of class Thing
> can be initialized with an initialiser list .... (as above).
>
>3. Section 12.6.2 Initializing Bases and Members - Says that the members
> can be initialised via the argument list (mem-initializer-list).
>
>
>I know you could always put a constructor into Thing, but this seems to
>reduce the orthogonality between use of aggregates.
>
>
>What is the situation ?
I believe that your attempted use of braced initializers either as
formal argument lists to constructors or as actual arguments in
function-like constructor calls (I can't really tell which you are
asking about) is/are invalid on syntactic grounds.
--
-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -
Author: rjl@f111.iassf.easams.com.au (Rohan LENARD)
Date: 24 Jun 1994 07:35:52 +1000 Raw View
In article <rfgCru6LC.Gzs@netcom.com>,
Ronald F. Guilmette <rfg@netcom.com> wrote:
>In article <2u8j2f$li6@f111.iassf.easams.com.au> rjl@f111.iassf.easams.com.au (Rohan LENARD) writes:
[ ..snip.. ]
>>I know you could always put a constructor into Thing, but this seems to
>>reduce the orthogonality between use of aggregates.
>>
>>
>>What is the situation ?
>
>I believe that your attempted use of braced initializers either as
>formal argument lists to constructors or as actual arguments in
>function-like constructor calls (I can't really tell which you are
>asking about) is/are invalid on syntactic grounds.
>
It seems to me that this would be a nice thing to allow, because aggregates
can be initialised as variables like this -
struct moocow {
int A;
double B;
double C;
};
moocow a = {1};
Where the first element is set to one, and the compiler is required to set
the rest of the elements to zero.
I would have though a mem-initalizer should have comprable features.
A minor change to the grammar in the arm would allow this -
******
BEFORE -
******
mem-initializer:
complete-class-name ( expression-list_opt_ )
identifier ( expression-list_opt_ )
since an expression-list is
expression-list:
assignment-expression
expression-list, assigment-expression
*****
AFTER -
*****
mem-initializer:
complete-class-name ( initializer-list_opt_ )
identifier ( initializer-list_opt_ )
since an initializer-list is
initializer-list:
assignment-expression
initializer-list , assignment-expression
{ initializer-list ,_opt_ }
Anyone like to comment further ?
Regards,
Rohan
--
------------------------------------------------------------------------
rjl@iassf.easams.com.au |
Rohan Lenard | .sig on holiday
+61-2-367-4555 |
Author: rjl@f111.iassf.easams.com.au (Rohan LENARD)
Date: 22 Jun 1994 15:43:43 +1000 Raw View
Hi folks,
Compare the initialisation of a struct within and out of a class -
// Struct outside of class
struct Thing {
double a;
int b;
};
Thing blob = { 10.0, -1 };
// Struct inside of class ?
class Foo {
public:
Foo() : ClassBlob({10.0, -1}) {};
// or should it be
// Foo() : ClassBlob({double(10.0),int(-1)}) {};
// or could it event be
// Foo() : ClassBlob(blob) {};
~Foo();
private:
Thing ClassBlob;
};
Is it possible to initialise ClassBlob like I have above, or not.
The ARM seems to suggest that it is possible, but then both compilers I tried
say it isn't. What are people thoughts ?
1. Section 8.4.1. Aggregates - basically says that Thing is an aggregate, and
thus can be initialised with an initialiser list starting and finishing
with matching braces. (But it's still an object)
2. Section 12.6 Initialization - also agrees that an object of class Thing
can be initialized with an initialiser list .... (as above).
3. Section 12.6.2 Initializing Bases and Members - Says that the members
can be initialised via the argument list (mem-initializer-list).
I know you could always put a constructor into Thing, but this seems to
reduce the orthogonality between use of aggregates.
What is the situation ?
Thanks in advance,
Regards,
Rohan
--
------------------------------------------------------------------------
rjl@iassf.easams.com.au |
Rohan Lenard | .sig on holiday
+61-2-367-4555 |
Author: jason@cygnus.com (Jason Merrill)
Date: Wed, 22 Jun 1994 21:18:04 GMT Raw View
>>>>> Rohan LENARD <rjl@f111.iassf.easams.com.au> writes:
> // Foo() : ClassBlob({double(10.0),int(-1)}) {};
> Is it possible to initialise ClassBlob like I have above, or not.
No; this is not allowed by the grammar. I have been thinking about
adding this to g++ as a GNU extension, since we already allow things like
casting an initializer list to a class type and such, but I haven't done
it yet.
Jason