簡體   English   中英

數字常量之前的預期primary-expression

[英]Expected primary-expression before numeric constant

對於以下代碼

#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:在函數'int main()'中:main.cpp:22:26:錯誤:在數字常量bool test5之前的預期primary-expression =反轉<1> 1> :: result;

如果我評論這些行它完美的工作示例與注釋錯誤行

解決方案評論后添加問題

因為我從評論中理解這是因為解析但為什么我沒有在bool test4 = Invert<1 < 1>::result;遇到問題bool test4 = Invert<1 < 1>::result; 為什么解析器這次很聰明

要允許正確的解析,您必須使用括號:

bool test5 = Invert<(1 > 1)>::result;

暫無
暫無

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

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