Struct constructor, opCall mess.
monarch_dodra
monarchdodra at gmail.com
Mon Feb 24 13:06:01 PST 2014
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.
More information about the Digitalmars-d-learn
mailing list