Default initialization of structs?
Lodovico Giaretta via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jun 17 03:53:40 PDT 2016
On Friday, 17 June 2016 at 10:50:55 UTC, Gary Willoughby wrote:
> I have a struct where I need to perform default initialization
> of some members but the compiler doesn't allow to define a
> default constructor which allow optional arguments.
>
> struct Foo(T)
> {
> private int _bar;
>
> this(int bar = 1)
> {
> this._bar = bar;
> }
> }
>
> auto foo = Foo!(string) // error
>
> Are there any patterns or idioms I could use to get the desired
> result? Or is it just the case if I use a constructor I have to
> pass values to it?
struct Foo(T)
{
private int _bar = 1;
this(int bar)
{
this._bar = bar;
}
}
auto foo = Foo!(string)();
This should do the trick.
More information about the Digitalmars-d-learn
mailing list