How to check if string is available at compile time

Meta jared771 at gmail.com
Thu Sep 21 13:52:25 UTC 2017


On Thursday, 21 September 2017 at 12:30:15 UTC, David Bennett 
wrote:
> On Thursday, 21 September 2017 at 11:42:36 UTC, David Bennett 
> wrote:
>> [snip]
>>
>> ```
>> string[] escapeCTFE(Args...)(){
>>
>>     static foreach (arg; Args){
>>         static if(__traits(compiles, ###WHATDOIPUTHERE###)){
>> [snip]
>
>
> So far the best I've come up with is :
>
> ```
>
> enum isCTstring(alias arg) = (!isAssignable!(typeof(arg)) || 
> __traits(compiles, mixin(` "foo" ~ `~__traits(identifier, 
> arg))));
>
> string[] escapeCTFE(Args...)(){
>
>     static foreach (arg; Args){
>         static if(isCTstring!(arg)){
>             pragma(msg, "Do work on string: ", arg);
>         }else{
>             pragma(msg, __traits(identifier, arg), " can only 
> be read at runtime");
>         }
>     }
>     return new string[32];
> }
>
> ```
>
> But this seems quite hackish... any better ideas?

Try __traits(compiles, { enum _ = arg; }). Creating an enum 
necessarily requires that its value is available at compile time.


More information about the Digitalmars-d-learn mailing list