簡體   English   中英

如何在C ++模板中使用比較表達式?

[英]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()
{}

我知道錯誤是由於編譯器將“大於”符號'>'作為模板終止符號。

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

將表達式放在括號中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM