Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用>;(大于)在模板参数内,但未获得解析错误?_C++_Parsing_Templates - Fatal编程技术网

C++ 如何使用>;(大于)在模板参数内,但未获得解析错误?

C++ 如何使用>;(大于)在模板参数内,但未获得解析错误?,c++,parsing,templates,C++,Parsing,Templates,我只想根据模板参数的大小定义函数: 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); } 模板 typename std::enable

我只想根据模板参数的大小定义函数:

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);
}
模板
typename std::enable_if 1,void>::type
美食(电视){
// ...
}
int main(){
//foobify((unsigned char)30);//不应编译
美食((长)30);
}
然而,我得到:

main.cpp:8:41: error: expected unqualified-id before numeric constant
     typename std::enable_if<sizeof(T) > 1, void>::type
main.cpp:8:41:错误:数字常量前应为非限定id
typename std::enable_if 1,void>::type
如果我改为执行
1
,它就会工作。因此,我相信GCC认为我是在结束模板参数,而不是继续布尔表达式


有没有什么方法可以不用处理而使用
本身?

是的,您应该使用括号:

template <class T>
typename std::enable_if<(sizeof(T) > 1), void>::type
foobify(T v) {
    // ...
}
模板
typename std::enable_if 1),void>::type
美食(电视){
// ...
}

是,使用该运算符的表达式必须用括号括起来。参见[临时名称]/3:

解析模板参数列表时,将第一个非嵌套的
138作为结束分隔符 而不是大于运算符。[…][示例:

模板类X{/*…*/};
X<1>2>x1;//语法错误
X2)>X2;//好的
-[结束示例]

138)一种
>
,它包含
动态强制转换
静态强制转换
重新解释强制转换
常量强制转换
,或包含 在本说明中,后续模板id的模板参数被视为嵌套的


显然,如果您使用该比较的对称对应项,即使用
也可以使用
更大的
typename std::enable\u if::type
:ppppppp,则这不适用
template<int i> class X { /* ...*/ };

X< 1>2 > x1; // syntax error
X<(1>2)> x2; // OK