How to check if string is available at compile time

David Bennett davidbennett at bravevision.com
Thu Sep 21 12:30:15 UTC 2017


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?


More information about the Digitalmars-d-learn mailing list