Re: struct field initialization

2017-08-16 Thread bitwise via Digitalmars-d-learn

On Wednesday, 16 August 2017 at 18:17:36 UTC, kinke wrote:

On Wednesday, 16 August 2017 at 18:11:05 UTC, bitwise wrote:
If I define a non-default constructor for a struct, are the 
fields initialized to T.init by the time it's called?


The struct instance is initialized with T.init before invoking 
the constructor.


Thanks for the quick response.

In regards to my second question, the "value = T(args);" variant 
seems to work, even with a const T, without calling a postblit - 
so I guess that's what I'll use.


Re: struct field initialization

2017-08-16 Thread bitwise via Digitalmars-d-learn

On Wednesday, 16 August 2017 at 18:11:05 UTC, bitwise wrote:

[...]


I'm asking this because I need to forward args to a container's 
node's value.


Something like this:


struct Node(T)
{
int flags;
T value; // maybe const

this(Args...)(int flags, auto ref Args args)
{
this.flags = flags;

// this?
emplace(, args);

// or this?
value = T(args);

// ?
}
}

struct Container(T)
{
Node!T[] nodes;

void add(Args...)(auto ref Args args)
{
int flags = 1234;
auto p = cast(Node!T*)malloc(Node!T.sizeof);
nodes ~= emplace(p, flags, args);
}
}



Re: struct field initialization

2017-08-16 Thread kinke via Digitalmars-d-learn

On Wednesday, 16 August 2017 at 18:11:05 UTC, bitwise wrote:
If I define a non-default constructor for a struct, are the 
fields initialized to T.init by the time it's called?


The struct instance is initialized with T.init before invoking 
the constructor.


struct field initialization

2017-08-16 Thread bitwise via Digitalmars-d-learn
If I define a non-default constructor for a struct, are the 
fields initialized to T.init by the time it's called? or am I 
responsible for initializing all fields in that constructor?


..or do structs follow the same rules as classes?
https://dlang.org/spec/class.html#field-init

  Thanks