0

Some strange problem with macro overload in Visual Studio.

#define ENUM_INTERNAL(A, ...) A, ENUM_INTERNAL(__VA_ARGS__)
#define ENUM_INTERNAL(A) A, COUNT

#define ENUM(name, ...) struct name { enum en {ENUM_INTERNAL(__VA_ARGS__)}; };

ENUM(xxxx, A1, A2, A3)
static const xxxx::en x = xxxx::A3; 
// All right, macro expanded into struct xxxx { enum en {A1, A2, A3, COUNT}; };

struct xxxx2 { enum en { ENUM_INTERNAL(A1, A2, A3) }; };
static const xxxx2::en x2 = xxxx2::A3; 
// Compile error: no such member. struct xxxx2 { enum en {A1, COUNT}; };

As I understand, actually there is no macro overloading by number of arguments. But somehow it works in first case, when we pass arguments through __VA_ARGS__ expression. Why it happens?

aaalex88
  • 619
  • 4
  • 13
  • Just curious: why don't you use 'enum class' instead of enum inside a struct? – roalz Feb 17 '17 at 10:26
  • From [this `#define` reference](http://en.cppreference.com/w/cpp/preprocessor/replace): "If the identifier is already defined as any type of macro, the program is ill-formed unless the definitions are identical." That means your program is *ill-formed* according to the standard. It is possible that MSVC++ have some kind of extension, but that still doesn't make it less "wrong" IMO. In short, that your technically illegal program behaves in unexpected ways is not really unexpected. :) – Some programmer dude Feb 17 '17 at 11:45

0 Answers0