How to use comparison expressions in C ++ templates?

#include <type_traits> template<int n> std::enable_if_t<n == 1, int> f() {} // OK template<int n> std::enable_if_t<n > 1, int> g() {} // VS2015 : error C2988: unrecognizable template declaration/definition int main() {} 

I know that the error is due to the fact that the compiler accepts the sign "more than" as a sign of completion of the template.

My question is: in this case, how to make the comparison expression legal?

+5
source share
1 answer

Put the expression in parentheses:

 #include <type_traits> template<int n> std::enable_if_t<(n == 1), int> f() { } template<int n> std::enable_if_t<(n > 1), int> g() { } int main() { } 
+7
source

All Articles