Static CT Factory

Engine Machine via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 18 20:25:06 PDT 2016


On Friday, 19 August 2016 at 02:54:48 UTC, Basile B. wrote:
> On Friday, 19 August 2016 at 01:53:22 UTC, Engine Machine wrote:
>> On Friday, 19 August 2016 at 01:25:10 UTC, Anonymouse wrote:
>>> On Friday, 19 August 2016 at 01:10:42 UTC, Engine Machine
>>>> x = 1.234;
>> Ok, well, I guess the error comes from something else.
>
> *x = 1.234 for when T verifies is(T == int*) produces an error.
>
> You can put an aditional argument in the function header:
>
> void foo(T)(auto ref T t = T.init)
> {
> 	static if (is(T == int))
> 	    auto x = new Thing!int;
> 	else static if (is(T == double))
> 	    auto x = new Thing!double;
> 	else
>             static assert(false, "incorrect type");
> 	*x = t;
> }

It was because I had a added a new type that I didn't account for 
in the static if chain ;/ The static assert is sort of required. 
We sort of need a static switch with required default so 
forgetting this stuff doesn't break later on(in strange ways) 
when new types are added.







More information about the Digitalmars-d-learn mailing list