Section 4.3 of C++ Templates states \'Not being able to use floating-point literals (and simple constant floating-point expressions) as template argum
A possible solution to this problem is to to use a type that has a constant value which is a float, then use that type as a template parameter. For example, if you want to have an integer polynomial, say, and want to evaluate it at some compile-time floating point value:
template
class Polynomial {
public:
template
static constexpr float eval() {
return a*t::value*t::value + b*t::value + c;
}
};
class THREE_POINT_FIVE {
public:
static constexpr float value = 3.5f;
};
int main() {
constexpr float y = Polynomial<2, 0, 1>::typename eval();
std::cout << y << std::endl;
}
One could also use helper classes that allow creating classes of floats, for example for percent:
template
class PERCENT {
public:
static constexpr float value = p * 0.01f;
};
...
constexpr float y2 = Polynomial<2, 0, 1>::typename eval>
...
I guess this is similar to using the std::ratio mentioned before.