问题
Imagine, I have a debug source file, which is like this:
#if _OWN_DEBUG_LEVEL != 0
void DebugLogMsg (DebugStruct_t *DebugStruct, size_t sizeID, char const *szFormat, ...);
#else
#define DebugLogMsg(_Expression1, _Expression2, _Expression3) ((void)0)
#endif
In this case I do not really care about the additional arguments to the function, but what about this case?
#if _OWN_DEBUG_LEVEL > 0
#undef DebugLogMsg1
#define DebugLogMsg1(_Expression1, _Expression2, _Expression3) \
DebugLogMsg(_Expression1, _Expression2, _Expression3)
#endif
In this case I'm not quite sure... when I call the macro like this:
DebugLogMsg1(pointer, var, pointer, 1, 2, 3);
will _Expression3
be treat as would it be pointer, 1, 2, 3
or what would be the exact behaviour?
回答1:
It just wouldn't work. You should use variadic macros:
#define DebugLogMsg1(a, b, c, ...) DebugLogMsg(a, b, c, __VA_ARGS__)
Or perhaps better (since it doesn't cause issues with trailing commas):
#define DebugLogMsg1(...) DebugLogMsg(__VA_ARGS__)
来源:https://stackoverflow.com/questions/20499983/how-to-does-a-variable-argument-functioncall-as-macro-define