Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 03, 2014 Struct template | ||||
---|---|---|---|---|
| ||||
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. |
November 03, 2014 Re: Struct template | ||||
---|---|---|---|---|
| ||||
Posted in reply to deed | 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.
|
November 03, 2014 Re: Struct template | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | > static if (is(typeof(T) == int))
>
> should be
>
> static if (is(T == int))
>
>
> T is already a type.
Ahh. Thanks!
|
November 04, 2014 Re: Struct template | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Monday, 3 November 2014 at 17:05:21 UTC, John Colvin wrote:
> static if (is(typeof(T) == int))
>
> should be
>
> static if (is(T == int))
>
>
> T is already a type.
I thought this was supposed to produce an error message rather than fail silently... I'm positive this used to be an error. Did it change?
|
Copyright © 1999-2021 by the D Language Foundation