isCallableCTFE trait to test whether an expression is callable during CTFE
Timothee Cour
thelastmammoth at gmail.com
Wed Jul 10 20:09:31 PDT 2013
On Wed, Jul 10, 2013 at 7:16 PM, Timothee Cour <thelastmammoth at gmail.com>wrote:
> template isCallableCTFE(alias fun){
> template isCallableCTFE_aux(alias T){
> enum isCallableCTFE_aux=T;
> }
> enum isCallableCTFE=__traits(compiles,isCallableCTFE_aux!(fun()));
> }
>
> template isCallableCTFE2(fun...){
> enum isCallableCTFE2=true;
> }
>
>
> unittest{
> int fun1(){
> return 1;
> }
> auto fun1_N(){
> import std.array;
> //would return Error: gc_malloc cannot be interpreted at compile time,
> because it has no available source code due to a bug
> return [1].array;
> }
> int fun2(int x){
> return 1;
> }
> auto fun2_N(int x){
> import std.array;
> //same as fun1_N
> return [1].array;
> }
>
> int a1;
> enum a2=0;
>
> static assert(!isCallableCTFE!(()=>a1));
> static assert(isCallableCTFE!(()=>a2));
>
> static assert(isCallableCTFE!fun1);
> static assert(!isCallableCTFE!fun1_N);
>
> static assert(isCallableCTFE!(()=>fun2(0)));
> static assert(!isCallableCTFE!(()=>fun2_N(0)));
> //NOTE:an alternate syntax which could be implemented would be: static
> assert(!isCallableCTFE!(fun2_N,0)));
> }
>
can we add this to std.traits?
it allows (among other things) to write unittests for CTFE, etc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130710/0cce7556/attachment-0001.html>
More information about the Digitalmars-d
mailing list