Initialization of structure field w/o default ctor

drug via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 22 04:45:27 PST 2015


On 22.01.2015 15:30, bearophile wrote:
> drug:
>
>> Also can I avoid "dummy" non-default ctor for Bar?
>
> One solution:
>
>
> struct Foo {
>      int foo;
>
>      @disable this();
>
>      this(int foo_) pure nothrow @safe @nogc {
>          this.foo = foo_;
>      }
> }
>
> struct Bar {
>      enum arraySize = 3;
>
>      Foo[arraySize] foo = Foo(1);
> }
>
> void main() @safe {
>      import std.stdio;
>
>      Bar bar;
>      bar.writeln;
> }
>
>
> Bye,
> bearophile

Yes, that's what the doctor prescribed. Thank you!


More information about the Digitalmars-d-learn mailing list