Reading through the C++17 standard, it seems to me that there is an inconsistency between pp-number
as handled by the preprocessor and numeric literals, e.g. user-defined-integer-literal
, as they are defined to be handled by the "upper" language.
For example, the following is correctly parsed as a pp-number
according to the preprocessor grammar:
123_e+1
But placed in the context of a C++11-compliant code fragment,
int operator"" _e(unsigned long long)
{ return 0; }
int test()
{
return 123_e+1;
}
the current Clang or GCC compilers (I haven't tested others) will return an error similar to this:
unable to find numeric literal operator 'operator""_e+1'
where operator"" _e(...)
is not found and trying to define operator"" _e+1(...)
would be invalid.
It seems that this comes about because the compiler lexes the token as a pp-number
first, but then fails to roll-back and apply the grammar rules for a user-defined-integer-literal
when parsing the final expression.
In comparison, the following code compiles fine:
int operator"" _d(unsigned long long)
{ return 0; }
int test()
{
return 0x123_d+1; // doesn't lex as a 'pp-number' because 'sign' can only follow [eEpP]
}
Is this a correct reading of the standard? And if so, is it reasonable that the compiler should handle this, arguably rare, corner case?