1

How do I go about correctly wrapping a Variadic function when I need to change the first parameter's type?

For instance:

void original_function(char* format, ...)
{
  // Other Code Here
}


void wrapper(string format, ...)
{
        va_list args;
        va_start(args, format);
        original_function(format.c_str(), args);
        va_end(args);
}

For some reason, the need to convert it and cast it to a c string causes the other variables to not be read correctly. How do I correctly pass this information on while changing types of the first parameter?

David
  • 1,648
  • 1
  • 16
  • 31
  • 1
    What is `Message` and why aren't you using `format` in that location ?? Fix that first, then see below (thanks Raymond). – WhozCraig May 03 '19 at 03:34
  • 5
    Use `vprintf` instead of `printf`. – Raymond Chen May 03 '19 at 03:34
  • @WhozCraig - Thanks, that was a typo. Fixed now – David May 03 '19 at 03:41
  • @RaymondChen - That works great, thanks! Still somewhat interested in the overarching question as to if its possible vs just calling a different method. I guess perhaps I should read more into what these methods actually do. Tested your solution and it works great – David May 03 '19 at 03:42
  • 2
    @David *"if its possible"* - if *what* is possible ? You were calling a non-variadic function and passing a `va_arg` stack, expecting variadic results. This isn't a question of possible vs. not. It's not possible to use the *wrong* function to do what you were trying. Using the right function makes all the difference in the world. – WhozCraig May 03 '19 at 03:46
  • It is not possible to generate a `...` from a `va_list`. Once a `...` has been captured into a `va_list`, then only thing you can do is consume it as a `va_list`, which means calling functions designed to accept `va_list`s.. However, since you appear to be using C++, you can use a variadic template. – Raymond Chen May 03 '19 at 04:24
  • 1
    Why is `wrapper`'s return type omitted? Why do you pass `string`s by value? – L. F. May 03 '19 at 05:29

0 Answers0