“operator”” _Bq”存在什么问题?

huangapple go评论70阅读模式
英文:

What is wrong with operator"" _Bq?

问题

[over.literal]上,我读到在示例列表中,以下内容是有效的:

double operator""_Bq(long double);

而以下内容是无效的:

double operator"" _Bq(long double);

这显然是因为""后面有空格。

现在,从链接页面,我可以轻松跳转到[usrlit.suffix]并阅读到:

不以下划线开头的文字后缀标识符已保留供未来标准化使用。

好的,但我在哪里可以找到有关operator"" _Bq为什么无效的解释呢?

我还阅读了cppreferenceuser-defined-string-literal的描述,但说实话,我觉得有点混乱。

有人可以用示例来解释一下吗?

英文:

At [over.literal] I read, in the list of examples, that

double operator""_Bq(long double);

is valid, whereas

double operator"" _Bq(long double);

is ill-formed, which is apparently a consequence of the space right after "".

Now, from the linked page, I can easily get to [usrlit.suffix] with one click and I read that

> Literal suffix identifiers that do not start with an underscore are reserved for future standardization.

Good, but where do I read why operator"" _Bq is invalid?

I've also read the description of user-defined-string-literal on cppreference, but to be honest I've found it a bit confusing.

Can anybody break that down with examples?

答案1

得分: 7

""" _Bq" 是一个由 字符串字面值 后跟一个 标识符 组成的,而 """_Bq" 是一个 用户定义的字符串字面值,它是一个单一且不同的预处理标记([lex.pptoken])。

因此,前者受宏展开的影响,但后者不受影响:

#define _x
int operator "" _x(char); // 变成 `int operator "" (char);`,这是无效的
int operator ""_x(char);  // 可行

除此之外,这两种形式都作为 字面值运算符标识符 被允许,并且具有相同的含义。然而,由于形成第一个构造涉及使用保留的 标识符,它根据 [lex.name]/3 被认为是非法的,不需要诊断(其背后的动机是 _Bq 可能被实现用作宏)。

这就是意图,但目前的规范措辞实际上并没有清晰说明这个差异:用户定义的字符串字面值 最终包含一个 标识符,而 [lex.name]/3 没有表明它仅适用于那些自身是预处理标记的标识符。这是 CWG2521 的主题。

英文:

"" _Bq is a string-literal followed by an identifier, whereas ""_Bq is a user-defined-string-literal which is a single, distinct preprocessing token ([lex.pptoken]).

As a consequence, the former is affected by macro expansion, but the latter is not:

#define _x
int operator "" _x(char); // becomes `int operator "" (char);` which is invalid
int operator ""_x(char);  // ok

That aside, both forms are allowed as part of a literal-operator-id and have the same meaning. However, since forming the first construct involves the use of a reserved identifier, it is ill-formed with no diagnostic required per [lex.name]/3 (motivation for this being that _Bq could be in use by the implementation as a macro).


That's the intent, anyway. The current normative wording does not actually make that difference clear enough: user-defined-string-literal ultimately contains an identifier, and [lex.name]/3 has no indication that it only applies to identifiers that are themselves preprocessing tokens. This is the subject of CWG2521.

huangapple
  • 本文由 发表于 2023年2月6日 17:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定