Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inhibit macro expansion

Tags:

c++

Is there any way to inhibit preprocessor macro expansion? I have an existing C header file that uses #define to define a set of integers and I would like to copy it to a C++ enum that has the same value names. For example (using C++11):

enum MyEnum {
  VALUE,
  // ...
};

#define VALUE 0

MyEnum convert(int x) {
  if (x == VALUE) {
    return MyEnum::VALUE;
  }
  // ...
}

The problem of course is that MyEnum::VALUE gets translated to MyEnum::0, which causes a syntax error. The best solution is to replace the macros with enums, but unfortunately that is not an option in my situation.

I tried to use concatenation, but that didn't help (the compiler gave the same error).

#define CONCAT(a,b) a##b
// ...
return MyEnum::CONCAT(VA,LUE);  // still results in MyEnum::0

Is there another solution that allows me to have the same name for the macro and for the enum value?

like image 949
Mark Lodato Avatar asked Oct 02 '13 05:10

Mark Lodato


Video Answer


1 Answers

You can undefine a macro:

#undef VALUE

after including the header.

like image 52
Adam Avatar answered Oct 25 '22 02:10

Adam