test if the alias of a template is a literal

Gianni Pisetta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 27 23:59:01 PDT 2016


On Friday, 28 October 2016 at 03:33:33 UTC, Basile B. wrote:
> Hello, I think the correct isStringLiteral would be:
>
> import
>     std.meta;
>
> template isStringLiteral(alias V)
> {
>     enum isCompileTime = is(typeof((){enum a = V;}));
>     enum isString = is(typeof(V) == string);
>     enum isStringLiteral = isCompileTime && isString;
> }
>

It works, Thanks. Also, i don't think in my case there is the 
need for a variant for types( aka isStringLiteral(V) without 
alias) because it's an error to pass a type to Optimize in first 
place. But for a general purpouse library, maybe a template 
isLiteral(alias V) that only checks if it is a literal without 
the type checking, would have more sense to have also 
isLiteral(V) for types that returns always false.

Gianni Pisetta


More information about the Digitalmars-d-learn mailing list