以下代码在 GCC 和 Clang 中编译:
long double operator""_a(long double);
auto x = 0e1_a+0; // OK
但不是这个(将 _a
替换为 _e
):
long double operator""_e(long double);
auto y = 0e1_e+0; // Error: unable to find numeric literal operator 'operator""_e+0'
OTOH,这段代码编译:
auto z = 0e1_e +0; // OK
这是怎么回事?
(本题灵感来自this GCC bug report。)
最佳答案
再次最大咀嚼罢工。
[lex.pptoken]/p3:
If the input stream has been parsed into preprocessing tokens up to a given character:
- [two exceptions not relevant here]
- Otherwise, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token, even if that would cause further lexical analysis to fail, except that a header-name (2.8) is only formed within a
#include
directive (16.2).
问题在于 0e1_e+0
与 0e1_a+0
不同,它是一个有效的预处理编号 ([lex.ppnumber]):
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 .
因此,0e1_e+0
被解析为单个 pp-number 预处理标记,然后由于无法转换为有效标记(例如显而易见的原因)。
0e1_a+0
被解析为三个标记,0e1_a
、+
和 0
,一切都很好。
关于c++ - 在表达式中使用用户定义的文字有时需要空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33670238/