views:

296

answers:

2

Help settle the debate that's going on in the comments at this question about bool and 1:

Can a standards-conforming C++ preprocessor allow one to use #define to redefine a language keyword? If so, must a standards-conforming C++ preprocessor allow this?

If a C++ program redefines a language keyword, can that program itself be standards conforming?

+6  A: 
James McNellis
So technically, in C++03 it is legal to redefine keywords, *as long as you do not include a single header*? I think I'm going to interpret it as "redefining keywords is forbidden", like C++0x says. :)
jalf
This answers the question of whether a program that redefines the keywords is standards conforming, not the technical capability of the preprocessor.
Ken Bloom
@Ken: Right. However, if a program violates that shall statement, then the program is ill-formed because the rule is diagnosable (i.e., it doesn't say "no diagnostic required" and doesn't say that violating it results in undefined behavior). Therefore, I think that a conforming preprocessor is at least required to warn that you are violating the rule.
James McNellis
@jalf, and note that, like James said, "header" in the Standard only refers to Standard library headers. You may still happily include your own source files. And note that in C++03 some keywords can't be `#define`'ed. These are `new` and `delete`. Now that C++0x forbids all of them, i suspect http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#369 has become resolved too. (though i'm not sure why they are treated specially in preprocessing phase).
Johannes Schaub - litb
+3  A: 

Working from the 2005-10-19 C++ working draft (since I don't have a standard handy):

Section 16.3 defines the grammar for #define to be #define identifier replacement-list-newline (object-like macros) or one of several constructions beginning with #define identifier lparen (function-like macros). identifiers are defined in section 2.10 to be identifier-nondigit | identifier identifier-nondigit | identifier digit. Section 2.11 indicates that a certain list of identifiers are unconditionally treated as keywords in phase 7 of compilation (section 2.1), and I conclude that they are therefore not treated specially in phase 4, which is preprocessor expansion. Thus, it appears that the standard requires the preprocessor to allow you to redefine language keywords (listed in Section 2.11).

However, the preprocessor has a keyword of its own, namely defined, as well as a list of predefined macros (Section 16.8). Section 16.8 states that the behavior is undefined if you redefine these, but does not prohibit the preprocessor from recognizing these as macro names.

Ken Bloom
Note also that the identifiers `true` and `false` (since there are no "keywords" yet, they are just "identifiers") are treated specially during macro replacement.
James McNellis
@James: can you point out the section in the standard (or the draft) where it says this? GCC's implementation also claims to treat C++'s named operators (those `#defined` in iso646.h in C) specially when operating in C++ mode. http://gcc.gnu.org/onlinedocs/cpp/Macros.html
Ken Bloom
@Ken: "After all replacements due to macro expansion and the defined unary operator have been performed, all remaining identifiers and keywords, except for `true` and `false`, are replaced with the pp-number 0" (16.1/4). As for why the named operators are handled specially, it is because a named operator is an "operator or punctuator" preprocessing token, not an "identifier" preprocessing token.
James McNellis
@James and the G++ team found it necessary to allow even these to be killed: http://stackoverflow.com/questions/2419805/when-did-and-become-an-operator-in-c/2419831#2419831
Joshua
@Ken: When I said "macro replacement" in my first comment, I meant "conditional inclusion evaluation." Sorry about that.
James McNellis
@James, so redefining keywords other than those mentioned in 16.8 which are nevertheless used by the preprocessor does change their meaning before phase 7, but the redefinition is ignored in phase 4 when evaluating `#if`.
Ken Bloom
There's a footnote that points out that there are no keywords during macro processing (phase 4). Note that 17.6.3.1.1 changed between C++95 and the latest Final Committee Draft, and the latter forbids redefining keywords (although in a nonintuitive place).
David Thornley
I think that's the important part here. The language in §17.6.3.3.1 (of the C++0x FCD) says "names lexically identical to keywords," so even though there are no keyword tokens during preprocessing, there can be other tokens (namely identifiers, but also op-or-puncs for the named operators) that are lexically identical to keywords (i.e., have the same spelling). Ergo, it should be trivial for the preprocessor to detect and diagnose a violation of the rule, and the preprocessor is not required to allow redefinition of keywords.
James McNellis
The C++0x final committee draft is at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf for those who are following along at home.
Ken Bloom
@James: §17.6.3.3.1 is qualified by §17.6.3.1 which states "This section describes restrictions on C++ programs that use the facilities of the C++ standard library."
Ken Bloom
@Ken: Ohhh, you are right; I totally missed that. So, I suppose C++03 and C++0x don't really differ after all (I'll have to update my answer to reflect that). However, given that the preprocessor also handles source file inclusion, and given that the names of the standard libraries are known, it should still be relatively easy for the preprocessor to detect whether the rule has been violated.
James McNellis