2016-07-16 87 views
5
#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() 
{} 

我知道錯誤是由於編譯器將「大於」符號'>'作爲模板終止符號。如何在C++模板中使用比較表達式?

我的問題是:在這種情況下,如何使比較表達式合法?

回答

7

放入括號中的表達式:

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