xmllmx
xmllmx

Reputation: 42297

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 the error is due to the compiler takes the "greater than" sign '>' as a template termination sign.

My question is: In such a case, how to make the comparison expression legal?

Upvotes: 6

Views: 183

Answers (1)

ildjarn
ildjarn

Reputation: 62975

Put the expression in parenthesis:

#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() { }

Upvotes: 8

Related Questions