dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Variadic Macros: can i control the macros arguments index?

  1. #1
    Join Date
    Apr 2009
    Posts
    1,230

    Variadic Macros: can i control the macros arguments index?

    when we create a macro with several arguments we use '...':
    Code:
    #define events(...) event2(__VA_ARGS__)
    but own can i see the arguments index for work with them separated or even use a loop?

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,417

    Re: Variadic Macros: can i control the macros arguments index?

    To extract the arguments you use va_arg, va_start and va_end. See https://msdn.microsoft.com/en-us/library/kb57fad8.aspx

    Note that it is easy to get these wrong. Very little error checking is done. That's one of the reasons why variadic templates were introduced.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.4)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,230

    Re: Variadic Macros: can i control the macros arguments index?

    i'm sorry, the va_arg, va_start and va_end can be used on macros?

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,417

    Re: Variadic Macros: can i control the macros arguments index?

    The pre-processor expands all macros before the compiler starts, so all the compiler sees is the already-expanded macros - not the macro definitions.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.4)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,230

    Re: Variadic Macros: can i control the macros arguments index?

    done.
    we must expand the macro:
    Code:
    #define EXPAND_0(...) __VA_ARGS__#define EXPAND_1(...) EXPAND_0(EXPAND_0(__VA_ARGS__))
    #define EXPAND_2(...) EXPAND_1(EXPAND_1(__VA_ARGS__))
    #define EXPAND(...) EXPAND_2(EXPAND_2(__VA_ARGS__))
    
    
    #define NOTHING
    #define STOP(...)
    #define IS_END_OF_LIST(...) _, STOP
    
    
    #define NEXT(test, next) NEXT1(IS_END_OF_LIST test, next)
    #define NEXT0(test, next, ...) next NOTHING
    #define NEXT1(test, next) NEXT0(test, next, _)
    
    
    #define TRANSFORM0(f, x, next, ...)                 \
      f(x) NEXT(next, TRANSFORM1)(f, next, __VA_ARGS__)
    #define TRANSFORM1(f, x, next, ...)                 \
      f(x) NEXT(next, TRANSFORM0)(f, next, __VA_ARGS__)
    
    
    #define TRANSFORM(f, ...)                                   \
      EXPAND(TRANSFORM1(f, __VA_ARGS__, (end of arguments), _))
    
    
    #define EVENT(x) virtual void x;
    #define DECLARE_EVENTS(...) TRANSFORM(EVENT, __VA_ARGS__)
    thank you so much for all.
    it's still confused to me, but now we can control all arguments like a loop
    thank you so much for

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)