On Friday, 24 January 2025 at 10:50:15 UTC, Dom DiSc wrote:
Is it possible in D to find out if the parameter given to a
function is a literal or a compile time constant?
Of course, if it has _only_ parameters known at compile time,
the function will be executed during CTFE, so I can check for
the execution time. But if it has also runtime-parameters, it
will only be executed during runtime. Is it still possible to
find out, which values are known at compile-time?
They should be immutable, but do they have some other, more
specific property?
completes solutions; probaly not, partial solutions well....
```d
import std;
bool isctfe(alias i)(){
bool _isctfe(alias i)(int i2){
i2=i;
return __ctfe;
}
static if(__traits(compiles,(){enum b=_isctfe!(i)(i);}())){
return true;
} else {
return false;
}
}
unittest{
int i;
isctfe!(i).writeln;
isctfe!(3).writeln;
}
void foo(alias i)(float f){
static if(isctfe!i){
"i is ct ".write;
} else {
"i isnt ct ".write;
}
writeln("f is ",f);
}
unittest{
int i;
foo!(3)(13.37);
foo!(i)(4.20);
}
```