test if the alias of a template is a literal
TheFlyingFiddle via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Oct 27 08:30:47 PDT 2016
On Thursday, 27 October 2016 at 14:45:22 UTC, Gianni Pisetta
wrote:
> On Thursday, 27 October 2016 at 14:34:38 UTC, TheFlyingFiddle
> wrote:
>> On Thursday, 27 October 2016 at 14:04:23 UTC, Gianni Pisetta
>> wrote:
>>> Hi all,
>>> but at the moment isStringLiteral will return true even with
>>> variables of type string. So i searched for a metod to check
>>> if an alias is a literal value, but found nothing. Anyone
>>> have any clue on how can be done?
>>>
>>> Thanks,
>>> Gianni Pisetta
>>
>> Not really understanding your problem. Could you include an
>> example use that is problematic?
>
> Yea, sorry I missed that.
> A really stupid example would be
>
> string var;
>
> alias Sequence = Optimize!( "The", " ", "value", " ", "of", "
> ", "var is ", var );
>
> static assert( is( Sequence == AliasSeq!( "The value of var is
> ", var ) ) );
>
> writeln( Sequence );
>
> given that you include the code snippet in the first post.
>
> Thanks, Gianni
I think this fixes the problem:
template isStringLiteral(T...) if (T.length == 1) {
static if(is( typeof(T[0]) == string ))
{
enum bool isStringLiteral = !__traits(compiles, &T[0]);
}
else
{
enum bool isStringLiteral = false;
}
}
Literals do not have an address but variables do.
However note that:
string var;
static assert(!is(AliasSeq!(var) == AliasSeq!(var)));
It still works at run-time though:
string var = " hello ";
alias Seq = Optimize!("This", " is", " a", " variable! ", var);
//pragma(msg, Seq) //Fails to compile at var
//static assert(is(Seq == AliasSeq!("This is a variable!",
var))); //Also fails
writeln(Seq); //Still works
More information about the Digitalmars-d-learn
mailing list