对于下面的代码
#include<iostream>
template<bool T>
class Invert
{
public:
static bool const result = !T;
};
int main()
{
bool test = Invert<1-1>::result;
std::cout << "test " <<test << "\n";
bool test1 = Invert<1 + 1>::result;
std::cout << "test1 " << test1 << "\n";
bool test2 = Invert<1 || 1>::result;
std::cout << "test2 " << test2 << "\n";
bool test3 = Invert<0 && 1>::result;
std::cout << "test3 " << test3 << "\n";
bool test4 = Invert<1 < 1>::result;
std::cout << "test4 " << test4 << "\n";
bool test5 = Invert<1 > 1>::result;//error error: expected primary-expression before numeric constant
std::cout << "test5 " << test5 << "\n";
return 0;
}
在第 22 行出现错误
main.cpp: In function 'int main()': main.cpp:22:26: error: expected primary-expression before numeric constant bool test5 = Invert<1 > 1>::result;
如果我评论这些行,它会完美地工作 Working example with commented error line
在解决方案评论后添加问题
由于我从评论中了解到这是因为解析
但为什么我在 bool test4 = Invert<1 < 1>::result;
中没有遇到问题?这次parser怎么这么聪明
最佳答案
要允许正确解析,您必须使用括号:
bool test5 = Invert<(1 > 1)>::result;
关于c++ - 数字常量之前的预期主表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46835131/