How to check if string is available at compile time

David Bennett davidbennett at bravevision.com
Thu Sep 21 21:51:39 UTC 2017


On Thursday, 21 September 2017 at 13:52:25 UTC, Meta wrote:
> 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:
>>
>> enum isCTstring(alias arg) = (!isAssignable!(typeof(arg)) || 
>> __traits(compiles, mixin(` "foo" ~ `~__traits(identifier, 
>> arg))));
>>
>> [snip]
>>
>> 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.

Ahh, warping it in {} seems to change the timing of the 
execution, this is starting to make a lot of sense to me now. 
(also explains why int[arg] works).

Thats much cleaner and I'll use that from now on.

Thanks a lot!
David


More information about the Digitalmars-d-learn mailing list