Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
December 03, 2011 C++ vs D aggregates | ||||
---|---|---|---|---|
| ||||
I recently stumbled on this thread: http://stackoverflow.com/ questions/5666321/what-is-assignment-via-curly-braces-called-and-can-it- be-controlled The important part is this: -------- 8< --------- begin --------- The Standard says in section §8.5.1/1, An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3). And then it says in §8.5.1/2 that, When an aggregate is initialized the initializer can contain an initializer-clause consisting of a brace-enclosed, comma-separated list of initializer-clauses for the members of the aggregate, written in increasing subscript or member order. If the aggregate contains subaggregates, this rule applies recursively to the members of the subaggregate. -------- >8 --------- end --------- Do D2 aggregates behave the same, or are there notable differences? |
December 04, 2011 Re: C++ vs D aggregates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dejan Lekic | On 03.12.2011 20:14, Dejan Lekic wrote:
> I recently stumbled on this thread: http://stackoverflow.com/
> questions/5666321/what-is-assignment-via-curly-braces-called-and-can-it-
> be-controlled
>
> The important part is this:
>
> -------- 8< --------- begin ---------
> The Standard says in section §8.5.1/1,
>
> An aggregate is an array or a class (clause 9) with no user-declared
> constructors (12.1), no private or protected non-static data members
> (clause 11), no base classes (clause 10), and no virtual functions (10.3).
>
> And then it says in §8.5.1/2 that,
>
> When an aggregate is initialized the initializer can contain an
> initializer-clause consisting of a brace-enclosed, comma-separated list
> of initializer-clauses for the members of the aggregate, written in
> increasing subscript or member order. If the aggregate contains
> subaggregates, this rule applies recursively to the members of the
> subaggregate.
> -------->8 --------- end ---------
>
> Do D2 aggregates behave the same, or are there notable differences?
Yes, struct static initializers are the same in D as in C++.
Differences are:
* D also has struct literals, which can be used in contexts other than initialization;
* There are no static initializers for classes. (D's classes are never 'aggregates' in the C++ sense);
* Static initializers for unions are currently very buggy in DMD.
|
December 04, 2011 Re: C++ vs D aggregates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dejan Lekic | Dejan Lekic Wrote:
> Do D2 aggregates behave the same, or are there notable differences?
D restricts usage to static initializers only, C++ doesn't have this limitation.
|
December 04, 2011 Re: C++ vs D aggregates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On 12/04/2011 12:00 PM, Kagamin wrote:
> Dejan Lekic Wrote:
>
>> Do D2 aggregates behave the same, or are there notable differences?
>
> D restricts usage to static initializers only, C++ doesn't have this limitation.
This works:
struct S{int x;}
void main(){
int a;
S x = {a};
}
What does not?
|
December 04, 2011 Re: C++ vs D aggregates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dejan Lekic | On 03-12-2011 20:14, Dejan Lekic wrote:
> I recently stumbled on this thread: http://stackoverflow.com/
> questions/5666321/what-is-assignment-via-curly-braces-called-and-can-it-
> be-controlled
>
> The important part is this:
>
> -------- 8< --------- begin ---------
> The Standard says in section §8.5.1/1,
>
> An aggregate is an array or a class (clause 9) with no user-declared
> constructors (12.1), no private or protected non-static data members
> (clause 11), no base classes (clause 10), and no virtual functions (10.3).
>
> And then it says in §8.5.1/2 that,
>
> When an aggregate is initialized the initializer can contain an
> initializer-clause consisting of a brace-enclosed, comma-separated list
> of initializer-clauses for the members of the aggregate, written in
> increasing subscript or member order. If the aggregate contains
> subaggregates, this rule applies recursively to the members of the
> subaggregate.
> -------->8 --------- end ---------
>
> Do D2 aggregates behave the same, or are there notable differences?
Does TDPL have a chapter on this? I think my searching skills may be failing me.
- Alex
|
December 07, 2011 Re: C++ vs D aggregates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | > What does not?
Yes, that kind of struct will work. :) Try to add a constructor...
|
Copyright © 1999-2021 by the D Language Foundation