Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use comparison expressions in C++ templates?

Tags:

c++

c++11

#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?

like image 314
xmllmx Avatar asked Jul 16 '16 10:07

xmllmx


1 Answers

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() { }
like image 198
ildjarn Avatar answered Sep 28 '22 01:09

ildjarn