Can I force compiler to accept only a constexpr
or a non-variable input to a function?
I am looking for allowing only compile time values to a function. Either using template or any other method.
Here, there is a working example for int
templates. The problem with double
s is that they cannot be used as template arguments.
#include <iostream>
template <double x>
void show_x()
{
std::cout<<"x is always "<<x<<" in the entire program."<<std::endl;
}
int main()
{
show_x<10.0>();
return 0;
}
error: ‘double’ is not a valid type for a template non-type parameter
Update
To those who have marked this question as a duplicate, I have to say:
I ask question
How to solve problem A?
and
Solution B does not work for problem A, I need another solution
Then you linked me to why solution B
does not work.
That is totally illogical.