This code is supposed to check if a float equals the half of an int at compile time using template meta-programming: #include
struct A {
constexpr static bool value = true;
};
struct B {
constexpr static bool value = false;
};
template<int i>
struct Meta {
constexpr static int value = i/2;
constexpr static bool func(float n) {
return std::conditional_t<n==value,A,B>::value;
}
};
int main( int argc, const char *argv[] ) {
constexpr bool b = Meta<4>::func(2);
std::cout << b << std::endl;
return 0;
}
But it refuses to compile. The compiler is saying that n is not a constant expression
:
test.cpp: In substitution of ‘template<bool _Cond, class _Iftrue, class _Iffalse> using conditional_t = typename std::conditional::type [with bool _Cond = (n == (float)2); _Iftrue = A; _Iffalse = B]’:
test.cpp:15:50: required from ‘static constexpr int Meta<i>::func(float) [with int i = 4]’
test.cpp:21:36: required from here
test.cpp:15:50: error: ‘n’ is not a constant expression
return std::conditional_t<n==value,A,B>::value;
^~~~~
test.cpp:15:50: note: in template argument for type ‘bool’
test.cpp: In function ‘int main(int, const char**)’:
test.cpp:21:36: in constexpr expansion of ‘Meta<4>::func((float)2)’
test.cpp:21:38: error: constexpr call flows off the end of the function
constexpr int b = Meta<4>::func(2);
^
What's the problem here? The value being passed to Meta::func
is a literal value. It should be treated like a constant.
What I'm interested in is how can I perform different actions based on a value at compile time. It should be possible because all the input required to calculate the output is available at compile time.
I want to know how can I perform different actions(which might involve types) based on a value at compile time. It should be possible because all the input required to calculate the output is available at compile time.