Struct template
John Colvin via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 3 09:05:20 PST 2014
On Monday, 3 November 2014 at 17:03:33 UTC, deed wrote:
> struct Internal { int i; double d; string s; }
>
> struct External_int {
> Internal internal;
> @property Internal* ptr () { return &internal; }
>
> this (int a)
> {
> internal.s = "int";
> internal.i = a;
> }
> }
>
> struct External (T) {
> Internal internal;
> @property Internal* ptr () { return &internal; }
>
> static if (is(typeof(T) == int))
> {
> this (T a)
> {
> internal.s = "int";
> internal.i = a;
> }
> }
> }
>
> void main ()
> {
> auto e1 = External_int(1); // Ok
> auto e2 = External!int(1); // Nope, cannot implicitly
> // convert expression (1)
> // of type int to Internal
> }
>
>
> Why? And how is this fixed? Thanks.
static if (is(typeof(T) == int))
should be
static if (is(T == int))
T is already a type.
More information about the Digitalmars-d-learn
mailing list