Struct constructor, opCall mess.

Remo remo4d at gmail.com
Mon Feb 24 15:34:47 PST 2014


On Monday, 24 February 2014 at 21:06:03 UTC, monarch_dodra wrote:
> On Monday, 24 February 2014 at 17:15:10 UTC, Remo wrote:
>> So what is proper/best way to mimic default constructor for 
>> struct ?
>
> Honestly, you can't, and you shouldn't try either. There "used" 
> to be the static opCall that allowed:
> ----
> auto a = T();
> ----
>
> But:
> a) This is being phased out: If T has a constructor, it will 
> seize to compile.
> b) It's not "default": "T a;" will still compile, but not 
> construct.
>
> The feedback I've been getting is that the "correct" way to 
> guarantee construction is to do it via a named factory pattern. 
> You disable the this(), so as to "force" initialization, and 
> make the constructors private. You'll get something along the 
> lines of:
>
> T a; //Nope.
> T a = T(); //Nope
> T a = T.build(); //OK!
> T a = T(1, 2); //Nope!
> T a = T.build(1, 2); //OK!
> T a = T.init; //OK! Special explicit requrest for 
> non-initialisation.
>
> With D's move semantics and (N)RVO, there should be 0 overhead 
> to do this.


Thanks, I will try to make this this way and look how well it 
will work.  But it could complicate connection from C++ to D and 
back.

Where I can find more info about 'D's move semantics'?
Apparently it is not the same as rvalue reference and move 
semantics in C++11 ?



More information about the Digitalmars-d-learn mailing list