5

The question came up of whether one should wrap extern "C" using #if or #ifdef. i.e.:

#if __cplusplus
extern "C" {
#endif

or

#ifdef __cplusplus
extern "C" {
#endif

Which begs the question: is there ever a situation where __cplusplus is defined to be equal to zero?

jpsamper
  • 51
  • 1

1 Answers1

3

According to the standard, the __cplusplus macro must be defined, the exact definition depends on the C++ standard being used but it will not be zero.

For example, for C++11 it must be 201103L, with the note "It is intended that future versions of this standard will replace the value of this macro with a greater value."

Historically, in some ancient non-conforming compilers you could probably dig up, __cplusplus was defined to 0 to indicate non-conformance with the standard. This is only of historical interest.

See: How are the __cplusplus directive defined in various compilers?

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • There were indeed such compilers, and Microsoft Visual C++ was one of them. – user207421 Sep 12 '17 at 03:41
  • @EJP interesting, so `#ifdef` would *not* work with that compiler, mistakenly indicating you're compiling a C++ source when it's actually C? – Mark Ransom Sep 12 '17 at 16:02
  • @EJP or are you actually saying that it defined `__cplusplus` as `0` for a C++ build? In that case you *definitely* have to use `#ifdef` because `#if` will fail. I'm getting even more confused now. – Mark Ransom Sep 12 '17 at 16:10
  • @MarkRansom: I believe EJP is just mentioning which compiler had this behavior. You don't have to worry about it unless you are interested in retrocomputing. – Dietrich Epp Sep 12 '17 at 16:26
  • @MarkRansom: Old Visual Studio C++ compilers were notoriously non-compliant, back in the Visual Studio 6 days and earlier, 20 years ago. Things are much better now. – Dietrich Epp Sep 12 '17 at 16:28
  • I still have Visual Studio 6 installed at home, although it has been a long time since I've used it. I had to keep it around for years to compile my MFC projects. – Mark Ransom Sep 12 '17 at 16:30