April 19, 2016 Re: Anonymous structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 18 April 2016 at 15:59:11 UTC, Steven Schveighoffer wrote:
> I wonder if it makes a difference for layout. So for example:
>
> struct T
> {
> struct
> {
> int x;
> ubyte y;
> }
> ubyte z;
> }
>
> If there is padding inserted between y and z.
There isn't. T.init.z.offsetof - T.init.y.offsetof == 1.
|
April 19, 2016 Re: Anonymous structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to captaindet | On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:
> not sure what you mean by "named substructure, not a nested structure" but this works:
>
> struct Outer{
> struct Inner{
> int x;
> int y;
> int z;
> }
> Inner inner;
> int a;
> }
>
> Outer outer;
> outer.a = 7;
> outer.inner.y = 42;
> // outer.x = 13; //fails
>
> writeln(outer);
Yeah thats basicly what I meant, just sort of tedious to have to write it like that, makes more complex layouts a real pain to write.
|
April 19, 2016 Re: Anonymous structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to captaindet | On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote: > On 2016-04-18 14:12, Tofu Ninja wrote: >> Also is there a way to have a named substructure, not a nested structure >> but something to just add an additional name, maybe something like >> struct a{ >> struct{ >> int x; >> int y; >> int z; >> } b; >> } > > not sure what you mean by "named substructure, not a nested structure" but this works: > > struct Outer{ > struct Inner{ > int x; > int y; > int z; > } > Inner inner; > int a; > } > > Outer outer; > outer.a = 7; > outer.inner.y = 42; > // outer.x = 13; //fails > > writeln(outer); There's another way: http://forum.dlang.org/post/n3q9vn$1l8g$1@digitalmars.com |
April 19, 2016 Re: Anonymous structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to ZombineDev | On Tuesday, 19 April 2016 at 16:16:39 UTC, ZombineDev wrote:
> On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:
>> On 2016-04-18 14:12, Tofu Ninja wrote:
>>> Also is there a way to have a named substructure, not a nested structure
>>> but something to just add an additional name, maybe something like
>>> struct a{
>>> struct{
>>> int x;
>>> int y;
>>> int z;
>>> } b;
>>> }
>>
>> not sure what you mean by "named substructure, not a nested structure" but this works:
>>
>> struct Outer{
>> struct Inner{
>> int x;
>> int y;
>> int z;
>> }
>> Inner inner;
>> int a;
>> }
>>
>> Outer outer;
>> outer.a = 7;
>> outer.inner.y = 42;
>> // outer.x = 13; //fails
>>
>> writeln(outer);
>
> There's another way:
> http://forum.dlang.org/post/n3q9vn$1l8g$1@digitalmars.com
How is that supposed to work here?
|
April 19, 2016 Re: Anonymous structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja | On Tuesday, 19 April 2016 at 17:16:00 UTC, Tofu Ninja wrote:
> On Tuesday, 19 April 2016 at 16:16:39 UTC, ZombineDev wrote:
>> On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:
>>> On 2016-04-18 14:12, Tofu Ninja wrote:
>>>> Also is there a way to have a named substructure, not a nested structure
>>>> but something to just add an additional name, maybe something like
>>>> struct a{
>>>> struct{
>>>> int x;
>>>> int y;
>>>> int z;
>>>> } b;
>>>> }
>>>
>>> not sure what you mean by "named substructure, not a nested structure" but this works:
>>>
>>> struct Outer{
>>> struct Inner{
>>> int x;
>>> int y;
>>> int z;
>>> }
>>> Inner inner;
>>> int a;
>>> }
>>>
>>> Outer outer;
>>> outer.a = 7;
>>> outer.inner.y = 42;
>>> // outer.x = 13; //fails
>>>
>>> writeln(outer);
>>
>> There's another way:
>> http://forum.dlang.org/post/n3q9vn$1l8g$1@digitalmars.com
>
> How is that supposed to work here?
struct A
{
template _b()
{
int x, y, z;
}
alias b = _b!();
}
void main()
{
import std.stdio;
auto a = A();
a.b.x = 5;
writeln(a.b.x); // prints 5
//writeln(a.b); // Error: expression has no value
}
|
April 19, 2016 Re: Anonymous structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to ZombineDev | On Tuesday, 19 April 2016 at 20:18:07 UTC, ZombineDev wrote:
> On Tuesday, 19 April 2016 at 17:16:00 UTC, Tofu Ninja wrote:
>> On Tuesday, 19 April 2016 at 16:16:39 UTC, ZombineDev wrote:
>>> On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote:
>>>> On 2016-04-18 14:12, Tofu Ninja wrote:
>>>>> Also is there a way to have a named substructure, not a nested structure
>>>>> but something to just add an additional name, maybe something like
>>>>> struct a{
>>>>> struct{
>>>>> int x;
>>>>> int y;
>>>>> int z;
>>>>> } b;
>>>>> }
>>>>
>>>> not sure what you mean by "named substructure, not a nested structure" but this works:
>>>>
>>>> struct Outer{
>>>> struct Inner{
>>>> int x;
>>>> int y;
>>>> int z;
>>>> }
>>>> Inner inner;
>>>> int a;
>>>> }
>>>>
>>>> Outer outer;
>>>> outer.a = 7;
>>>> outer.inner.y = 42;
>>>> // outer.x = 13; //fails
>>>>
>>>> writeln(outer);
>>>
>>> There's another way:
>>> http://forum.dlang.org/post/n3q9vn$1l8g$1@digitalmars.com
>>
>> How is that supposed to work here?
>
> struct A
> {
> template _b()
> {
> int x, y, z;
> }
> alias b = _b!();
> }
>
> void main()
> {
> import std.stdio;
>
> auto a = A();
> a.b.x = 5;
>
> writeln(a.b.x); // prints 5
> //writeln(a.b); // Error: expression has no value
> }
Also functions defined in _b can access members of A.
|
April 19, 2016 Re: Anonymous structure | ||||
---|---|---|---|---|
| ||||
Posted in reply to ZombineDev | On Tuesday, 19 April 2016 at 20:19:37 UTC, ZombineDev wrote: > On Tuesday, 19 April 2016 at 20:18:07 UTC, ZombineDev wrote: >> On Tuesday, 19 April 2016 at 17:16:00 UTC, Tofu Ninja wrote: >>> On Tuesday, 19 April 2016 at 16:16:39 UTC, ZombineDev wrote: >>>> On Monday, 18 April 2016 at 23:00:42 UTC, captaindet wrote: >>>>> On 2016-04-18 14:12, Tofu Ninja wrote: >>>>>> Also is there a way to have a named substructure, not a nested structure >>>>>> but something to just add an additional name, maybe something like >>>>>> struct a{ >>>>>> struct{ >>>>>> int x; >>>>>> int y; >>>>>> int z; >>>>>> } b; >>>>>> } >>>>> >>>>> not sure what you mean by "named substructure, not a nested structure" but this works: >>>>> >>>>> struct Outer{ >>>>> struct Inner{ >>>>> int x; >>>>> int y; >>>>> int z; >>>>> } >>>>> Inner inner; >>>>> int a; >>>>> } >>>>> >>>>> Outer outer; >>>>> outer.a = 7; >>>>> outer.inner.y = 42; >>>>> // outer.x = 13; //fails >>>>> >>>>> writeln(outer); >>>> >>>> There's another way: >>>> http://forum.dlang.org/post/n3q9vn$1l8g$1@digitalmars.com >>> >>> How is that supposed to work here? >> >> struct A >> { >> template _b() >> { >> int x, y, z; >> } >> alias b = _b!(); >> } >> >> void main() >> { >> import std.stdio; >> >> auto a = A(); >> a.b.x = 5; >> >> writeln(a.b.x); // prints 5 >> //writeln(a.b); // Error: expression has no value >> } > > Also functions defined in _b can access members of A. And also: import std.traits; writeln(Fields!A.stringof); // prints () writeln(Fields!Outer.stringof); // prints (Inner, int) |
Copyright © 1999-2021 by the D Language Foundation