3

I have a va_list which has a single entry in it. The entry is an integer 'hostObject'. I need to add a second one to this va_list which is going to be a pointer to another function which I plan to invoke at a later point of time. The sample code is as below:

va_list adjustVarArgs( tag_t hostObject, ... )
{
    va_list adjustedArgs;

    // TODO: Somehow make 'adjustedArgs' va_list to contain the hostObject and the function pointer.

    return adjustedArgs;
}

int Cfg0VariantConfigSavePost( METHOD_message_t * msg, va_list args )
{
    va_list largs;
    va_copy( largs, args );
    tag_t hostObject = va_arg( largs, tag_t );
    va_end( largs );

    va_list adjustedArgs = adjustVarArgs( hostObject );
    return Fnd0VariantConfigSavePost( msg, adjustedArgs );

    return ITK_ok;
}

deleteExprObjects is a method which I am interested in. On the whole, I need to store 1. hostObject 2. pointer to function: deleteExprsCallBack.

Please let me know how this can be done.

Thanks, Pavan.

Vinay Shukla
  • 1,818
  • 13
  • 41
Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47
  • 1
    The short and only answer is that you can't. A `va_list` is basically nothing more than a pointer into the stack, which means you can't return it from a function as once the function returns the arguments in the "list" doesn't really exist any more. – Some programmer dude Apr 20 '15 at 11:12
  • 1
    @JoachimPileborg It's a bit more complicated than just a pointer to the stack these days; see http://stackoverflow.com/questions/4958384/what-is-the-format-of-the-x86-64-va-list-structure/4958507#4958507 The point you're making still stands, though. – Wintermute Apr 20 '15 at 11:22

1 Answers1

1

According to the C Standard (7.16), the complete list of available macros operating on a va_list is:

The type declared is
va_list
which is a complete object type suitable for holding information needed by the macros va_start, va_arg, va_end, and va_copy.

None of those 4 can add an element to a va_list so this is not possible.

But since this C++, if you're using C++11, you could take a variadic argument pack and forward it along with your function pointer attached:

template <typename... Args>
int Cfg0VariantConfigSavePost( METHOD_message_t * msg, tag_t hostObject, Args&&... args)
{
    return Fnd0VariantConfigSavePost(msg,
        some_func_ptr,
        hostObject,
        std::forward<Args>(args)...);
}
Barry
  • 286,269
  • 29
  • 621
  • 977