Is every “normal” use of user-defined literals undefined behavior?

前端 未结 5 1760
遥遥无期
遥遥无期 2021-01-18 03:07

User defined literals must start with an underscore.

This is a more or less universally well-known rule that you can find on every layman-worded site talkin

5条回答
  •  一生所求
    2021-01-18 03:46

    This is a good question, and I'm not sure about the answer, but I think the answer is "no, it's not UB" based on a particular reading of the standard.

    [lex.name]/3.2 reads:

    Each identifier that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

    Now, clearly, the restriction "as a name in the global namespace" should be read as applying to the entire rule, not just to how the implementation may use the name. That is, its meaning is not

    "each identifier that begins with an underscore is reserved to the implementation, AND the implementation may use such identifiers as names in the global namespace"

    but rather,

    "the use of any identifier that begins with an underscore as a name in the global namespace is reserved to the implementation".

    (If we believed the first interpretation, then it would mean that no one could declare a function called my_namespace::_foo, for example.)

    Under the second interpretation, something like a global declaration of operator""_foo (in the global scope) is legal, because such a declaration does not use _foo as a name. Rather, the identifier is just a part of the actual name, which is operator""_foo (which does not start with an underscore).

提交回复
热议问题