cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[lex.name] p3 The using of preprocessing-token in translation phase 4 violates the rule #427

Open xmh0511 opened 10 months ago

xmh0511 commented 10 months ago

Full name of submitter (unless configured in github; will be published with the issue): Jim X

[lex.name] p3 says

In addition, some identifiers appearing as a token or preprocessing-token are reserved for use by C++ implementations and shall not be used otherwise; no diagnostic is required.

  • Each identifier that contains a double underscore __ or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.

Consider this case:

int main(){
  auto s =  __cplusplus; // #1
}

At #1, we use the macro name __cplusplus in translation phase 4, which is a preprocessing-token at that time. That means, we can never use any predefined macro names.

Suggested Resolution

"use" in this phrasing has many interpretations, presumably, the rule just wants to prohibit users to introduce that name rather than use them?

frederick-vs-ja commented 10 months ago

I've opened #226 for this.

BTW, I think it's intended to disallow user codes to call functions with reserved names that are implementations details of the standard library, since such calls may break invariants.

frederick-vs-ja commented 10 months ago

~Perhaps we should explicitly allow using standard macros with reserved names in any way except for #define and #undef. It is explicitly undefined behavior in C to #undef such a macro.~

Edit: [cpp.predefined] p4 has already achieved the same thing (see also CWG2581). So for the core wording, I think we can just exclude predefined macros in [lex.name] p3.