0

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 Answer 1

2

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__)
5
  • But __VA_ARGS__ isn't a c99 thing, is it?! Or do I jsut don't get your example?
    – dhein
    Dec 10, 2013 at 16:38
  • @Zaibis: It's very much a C99 thing.
    – Kerrek SB
    Dec 10, 2013 at 16:38
  • just opened my ISO/IEC 9899 and ctrl + f'ed for __VA_ARGS__, but I got no matches and never saw that before... and the syntax it self looks for me more like gnu99 attribute.
    – dhein
    Dec 10, 2013 at 16:39
  • @Zaibis: Look again. 6.10.3.1/2.
    – Kerrek SB
    Dec 10, 2013 at 16:43
  • You are right, I found it jsut right now by my self, the under scores of 6.10.3 5 are different, so I didn't get the match in my pdf ... Thanks dude.
    – dhein
    Dec 10, 2013 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.