问题
Are there any differences in behaviour between the C and C++ preprocessors?
They are defined by different passages of standards text (section 6.10 of the C standard and section 16 of the C++ standard).
My motivation for asking this is that a proposal for making the single quote a digit separator that was recently accepted into C++14 extends the C++ preprocessor grammar to accomodate this change (specifically, it extends the definition of a pp-number), and I'm wondering whether this introduces an incompatibility between the C and C++ preprocessors, and if so, whether it's the first feature to do so.
回答1:
There are some differences between the C and C++ processors. Ignoring the differences in predefined macros and available system headers, some differences that come to mind in the current versions of C and of C++ are:
andand friends are operators, not identifiers, in C++. This means#define and &&is valid in C, but not in C++, and means#if 1 and 2is valid in C++, but not in C (unlessandis suitably defined as a macro).falseandtrueare allowed in C++#ifexpressions, but replaced by0(like all identifiers) in C. This means#if true/C++/#else/C/#endifexpands to eitherC++, orC, depending on the language. Unlikeandand friends, though, these are not operators, so may be redefined by#definein either language.::,.*, and->*are tokens in C++. As a result of that, the##operator can be used to form them in C++, but not in C.- Raw string literals are available in C++, but not in C. As a result, given a macro
foo,R"x("foo")x"expands the macro in C, but not in C++. - Hexadecimal floating-point constants are available in C, but not in C++. As a result, given a macro
foo,0x1p+fooexpands the macro in C++, but not in C.
来源:https://stackoverflow.com/questions/21515608/what-are-the-differences-between-the-c-and-c-preprocessors