Hariom Singh
Hariom Singh

Reputation: 3632

Expected primary-expression before numeric constant

For the below code

#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;
}

Getting error at line no 22

main.cpp: In function 'int main()': main.cpp:22:26: error: expected primary-expression before numeric constant bool test5 = Invert<1 > 1>::result;

if I comment those lines it works perfectly Working example with commented error line

Added question after solution comments

Since I understood from the comments that it was because of parsing but why didn't I got problem in bool test4 = Invert<1 < 1>::result; How come parser was smart this time

Upvotes: 1

Views: 2608

Answers (1)

Jarod42
Jarod42

Reputation: 217135

To allow correct parsing, you have to use parenthesis:

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

Upvotes: 3

Related Questions