c++ - 数字常量之前的预期主表达式

标签 c++

对于下面的代码

#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/

相关文章:

c++ - 计算文件中的换行符

c++ - 为什么在从 win32 计时器回调中抛出时不调用我的析构函数?

c++ - 为什么大于 128 的对齐会表现得很奇怪?

c++ - ctor 声明/定义中接受的 const 限定符(llvm 错误?)

c++ - 不可复制和不可移动一起?

python - OpenCV 在 Python 和 C++ 实现中对 HOG 对象检测的不同结果

c++ - 频繁访问文件映射内存

c++ - 获取系统字体

c++ - QGraphicsWidget 作为 QObject 的 child

c++ - 作为 Web 客户端的 c++ 中的 gui 库