undefine and redefine the __cplusplus macro

1.7k views Asked by At

I want to undefine and redefine the __cplusplus macro am getting however compilation error:__cplusplus was not declared in this scope

#define TEMP_VERSION __cplusplus // temporary macro to hold the __cplusplus  containt
#undef __cplusplus
#define __cplusplus TEMP_VERSION  // redefine __cplusplus

#define AA 111
#undef AA
#define AA 111

int main()
{
    cout << "__cplusplus = "<<__cplusplus<<endl; // It doesn't work
    cout << "AA = "<<AA<<endl; // It works

    return 0;
}
  • Question 1: Why it is not working with the __cplusplus but works with AA

I know that this is really ugly but the reason why am undefining the macro is that I am using a third party Software where they are missusing the #ifdef __cplusplus, I mean the content of the #ifdef __cplusplus ... #endif is wrong. So instead of changing their software I choosed to do the following

#define TEMP_VERSION __cplusplus
#undef __cplusplus
#include <third_party_sw.h>
#define __cplusplus TEMP_VERSION
  • Question 2: What do you think is it a good approach ?
3

There are 3 answers

0
eerorika On BEST ANSWER

Question 1: Why it is not working with the __cplusplus but works with AA

Because __cplusplus is a pre-defined macro, and behaviour of (un-)defining it is undefined:

stadard draft [cpp.predefined]

If any of the pre-defined macro names in this subclause, or the identifier defined, is the subject of a #define or a #undef preprocessing directive, the behavior is undefined. ...

AA is not a pre-defined macro, nor is the indentifier reserved. It is OK to (re-)define it.


Question 2: What do you think is it a good approach ?

(Re-)defining reserved identifiers is never a good approach. Even redefining user defined macros is a dubious proposition.

Ideal approach might be to fix the third party software, if possible. Alternatively, you could write a custom header for the software which doesn't have the problem.

2
Jens Finkhaeuser On

It's because __cplusplus is pre-defined by the compiler.

Technically, it's the pre-processor that defines them, but depending on which compiler invokes the pre-processor, the output may differ. Try running this:

$ g++ -dM -E - </dev/null

The output is all the pre-processor defines that (in this case) the pre-processor run by this command produces.

Mind you __cplusplus does not appear there. It's built-in for every C++ compilation unit.

(The same works when substituting clang++ for g++.)

0
Sneftel On

From the C++ standard ([cpp.predefined, 3-4]):

The values of the predefined macros (except for __FILE__ and __LINE__) remain constant throughout the translation unit. If any of the pre-defined macro names in this subclause, or the identifier defined, is the subject of a #define or a #undef preprocessing directive, the behavior is undefined.

In other words, the __cplusplus macro isn't just something that the compiler starts out by defining before handing control over to you; it is an immutable constant, which cannot be changed and is an error to attempt to change.

As for whether it would be a good approach if it were possible: Depends on the context, but probably not. These sorts of "fool the header" hacks (#define private public, anyone?) are seldom effective and never safe.

EDIT: Oh, also incidentally, even if you did have a macro it was legal to save/restore, that wouldn't be the right way to do it. See Can I redefine a C++ macro then define it back?.