Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't "0xe+1" compile?

Look at this code snippet:

int a = 0xe+1;

Clang, gcc, icc don't compile this:

t.cpp:1:12: error: invalid suffix '+' on integer constant

MSVC successfully compiles.

Which compiler is correct? If clang and gcc are correct, why is this happening?

Note: if I add a space before +, the code compiles. If I change 0xe to 0xf, it compiles too. Maybe this has to do something with exponential notation (like 1.2e+3)?

like image 811
geza Avatar asked Mar 28 '18 20:03

geza


Video Answer


1 Answers

0xe+1 is treated as a single "preprocessing number" preprocessing token. This tokenization rule doesn't quite line up with the definition of numeric literals in the ordinary grammar; preprocessing numbers are defined as

pp-number:
    digit
    . digit
    pp-number digit
    pp-number identifier-nondigit
    pp-number ' digit
    pp-number ' nondigit
    pp-number e sign
    pp-number E sign
    pp-number p sign
    pp-number P sign
    pp-number .

If the tokenization rules were based on the numeric literal definitions instead of the simpler "preprocessing number" definition, your expression would be tokenized as 0xe + 1, but since the rules don't match up, you get a single 0xe+1 token, which is not a valid literal.

like image 169
user2357112 supports Monica Avatar answered Oct 18 '22 03:10

user2357112 supports Monica