我只想根据模板参数的大小定义一个函数:
template <class T>
typename std::enable_if<sizeof(T) > 1, void>::type
foobify(T v) {
// ...
}
int main() {
//foobify((unsigned char)30); // should not compile
foobify((long)30);
}
但是,我得到:
main.cpp:8:41: error: expected unqualified-id before numeric constant
typename std::enable_if<sizeof(T) > 1, void>::type
如果我改为 1 < sizeof(T)
就可以了.因此,我相信 GCC 认为我正在结束模板参数,而不是继续 bool 表达式。
有什么方法可以使用>
自己而无需解决它?
最佳答案
是的,使用该运算符的表达式必须加括号。见 [temp.names]/3:
When parsing a template-argument-list, the first non-nested
>
138 is taken as the ending delimiter rather than a greater-than operator. [..] [ Example:template<int i> class X { /* ...*/ }; X< 1>2 > x1; // syntax error X<(1>2)> x2; // OK
— end example ]
138) A
>
that encloses the type-id of adynamic_cast
,static_cast
,reinterpret_cast
orconst_cast
, or which encloses the template-arguments of a subsequent template-id, is considered nested for the purpose of this description.
显然,如果您使用该比较的对称对应物,即使用 <
,则不适用。 , 相反 - 在这种情况下解析是明确的。
关于c++ - 如何在模板参数中使用 >(大于)而不出现解析错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33423702/