August 25, 2014 Re: Error with constraints on a templated fuction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Mon, Aug 25, 2014 at 05:10:18PM +0000, via Digitalmars-d-learn wrote: > On Monday, 25 August 2014 at 17:05:48 UTC, H. S. Teoh via Digitalmars-d-learn wrote: > >On Mon, Aug 25, 2014 at 03:48:10PM +0000, Jeremy DeHaan via Digitalmars-d-learn wrote: > >>I've done things like this before with traits and I figured that this way should work as well, but it gives me errors instead. Perhaps someone can point out my flaws. > >> > >>immutable(T)[] toString(T)(const(T)* str) > >> if(typeof(T) is dchar)//this is where the error is > > > >The correct syntax is: > > > > if (is(typeof(T) == dchar)) > > Almost... T is already a type; typeof(T) doesn't compile. Ah, right, it should be simply: if (is(T == dchar)) Should've read more carefully before replying. :-P T -- It only takes one twig to burn down a forest. |
August 25, 2014 Re: Error with constraints on a templated fuction | ||||
---|---|---|---|---|
| ||||
On 08/25/14 18:52, Jonathan M Davis via Digitalmars-d-learn wrote: > Another commonly used one is is(typeof(foo)). typeof(foo) gets the type of foo > and will result in void if foo doesn't exist, and is(void) is false, whereas D is not quite that simple. ;) static assert(is(void)==true); (a) `typeof(invalid)` is an error; (b) the `is(...)` expression swallows errors; hence (a)+(b) -> static assert(is(typeof(invalid))==false); artur |
Copyright © 1999-2021 by the D Language Foundation