I'm trying to write an overloaded macro to format StringMap
entries, and cannot get the preprocessor to recognize when there is more than 1 argument. I have looked at virtually all similar questions and have tried 5+ implementations but none work. For example, this question.
My implementation is identical to the accepted answer, as follows:
#define STRING_MAP_ENTRY_1(a) \
{ a, #a }
#define STRING_MAP_ENTRY_2(a, b) \
{ a, b }
#define STRING_MAP_ENTRY( ... ) VA_SELECT( STRING_MAP_ENTRY, __VA_ARGS__ )
#define CAT( A, B ) A ## B
#define SELECT( NAME, NUM ) CAT( NAME ## _, NUM )
#define GET_COUNT( _1, _2, _3, _4, _5, _6, COUNT, ... ) COUNT
#define VA_SIZE( ... ) GET_COUNT( __VA_ARGS__, 6, 5, 4, 3, 2, 1 )
#define VA_SELECT( NAME, ... ) SELECT( NAME, VA_SIZE(__VA_ARGS__) )(__VA_ARGS__)
This works for a single argument call such as enum strings
below, but for manual_strings
I get the warning warning C4002: too many arguments for function-like macro invocation 'STRING_MAP_ENTRY_1'
. Which tells me it's not working properly because it's not calling the _2
version.
static StringMap<uint8> const enum_strings = {
STRING_MAP_ENTRY(ENUM_VALUE_1),
STRING_MAP_ENTRY(ENUM_VALUE_2)};
static StringMap<uint8> const manual_strings = {
STRING_MAP_ENTRY(0, "ENTRY_0"),
STRING_MAP_ENTRY(1, "ENTRY_1")};
I get this same warning trying every other implementation I've seen in similar questions on SO. Any idea why it's not working? I've seen a lot of notes here and there referring to compiler versions. Could this be the cause? The project is compiled using default MSVC compiler from VS 2017. I have included the C tag because, although this is in C++, the macro definitions are all valid C.