Distinguishing between const and non-const variable in a template?

renoX renosky at free.fr
Thu Feb 22 12:23:44 PST 2007


mario pernici a écrit :
> renoX Wrote:
> 
>> Hello,
>>
>> I'm trying to improve format string by allowing the format
>> " ... %{x} ...", my problem is that when I give a non-const char[] 
>> parameter in
>> mixin(Putf!(foo));
>>
>> then the template fail..
>>
>> How can I reliably detect in a template if the parameter is a constant 
>> or not?
[cut]
> 
> Maybe you can use something like this
> 
>   const char[] s = "ab";
>   static if(is(typeof(&s))) {
>     writefln(s, " not constant");
>   }
>   else {
>     static if(is(typeof(s))) {
>       writefln(s, " constant");
>     }
>   }

Very nice, thanks!

I'm curious: how did you find this??
I don't recall seeing it in the documentation and it looks kind of 
magical to me..

Thanks again,
renoX



More information about the Digitalmars-d-learn mailing list