Is a C++ preprocessor identical to a C preprocessor?

前端 未结 3 1004
情书的邮戳
情书的邮戳 2020-11-28 12:37

I am wondering how different the preprocessors for C++ and C are.

The reason for the question is this question on a preprocessor-specific question w

相关标签:
3条回答
  • 2020-11-28 13:17

    The C++03 preprocessor is (at least intended to be) similar to the C preprocessor before C99. Although the wording and paragraph numbers are slightly different, the only technical differences I'm aware of between the two are that the C++ preprocessor handles digraphs and universal character names, which are not present in C.

    As of C99, the C preprocessor added some new capabilities (e.g., variadic macros) that do not exist in the current version of C++. I don't remember for sure, but don't believe that digraphs were added.

    I believe C++0x will bring the two in line again (at least that's the intent). Again, the paragraph numbers and wording won't be identical, but I believe the intent is that they should work the same (other than retaining the differences mentioned above).

    0 讨论(0)
  • 2020-11-28 13:22

    They are supposed to be the same: C++98 and C++03 should match C90, and C++0x should match C99. There may be bugs in the wording, though.

    0 讨论(0)
  • 2020-11-28 13:24

    Predefined macros differ between the preprocessors, mostly for obvious language feature differences. E.g. compare:

    • C99 N1256 draft 6.10.8 "Predefined macro names"
    • C++11 N3337 draft 16.8 "Predefined macro names"

    In particular:

    • C requires you not to define __cplusplus, C++ uses it to represent the version
    • C uses __STDC__ to represent the version, C++ says is implementation defined and uses __cplusplus instead
    • C has __STDC_IEC_559__ and __STDC_IEC_559_COMPLEX__ to indicate floating point characteristics, C++ does not and seems replace that with the per type std::numeric_limits<float>::is_iec559 constants
    • C does not have the macros prefixed with __STDCPP: _STDCPP_STRICT_POINTER_SAFETY__ and __STDCPP_THREADS__

    As mentioned by DevSolar, C11 added many more defines which are not part of C++11.

    0 讨论(0)
提交回复
热议问题