How to does a variable argument Functioncall as macro define?

余生颓废 提交于 2019-12-12 02:39:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!