Prohibit Macro Volume

Is there a way to block the extension of a macro processor? 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 translates to MyEnum::0 , which causes a syntax error. The best solution is to replace macros with enumerations, but, unfortunately, this is not an option in my situation.

I tried using concatenation but that did not 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 a macro and for an enumeration value?

+6
source share
1 answer

You can define a macro:

 #undef VALUE 

after including the header.

+3
source

Source: https://habr.com/ru/post/955076/


All Articles