I was reading an article about non-type template arguments, and it said that :
When being instantiated, only compile time constant integer can be passed. This means 100, 100+99, 1<<3 etc are allowed, since they are compiled time constant expressions. Arguments, that involve function call, like abs(-120), are not allowed.
Example :
template<class T, int SIZE>
class Array{};
int main(){
Array<int, 100+99> my_array; // allowed
Array<int, abs(-120)> my_array; // not allowed
}
what’s the difference between 100+99 and abs(-120) ?
how come 100+99 are compiled time and abs(-120) is not?
100+99is optimized out to199at compile time.abs()is function and it may or may not be markedconstexpr(C++11 feature, that would allow you to do so; you can easily check cppreference or standard to see if it’sconstexprin C++11). It requires to be executed; compiler cannot deduce that it’s state less function returning same value for every run with same argument.