Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable arguments to another function that accepts a variable argument list

People also ask

Can we pass a variable argument list to a function at?

Every actual argument list must be known at compile time. In that sense it is not truly a variable argument list.

How do you pass an argument from one function to another?

How about wrapping that function with another function in order to create a function scope that you can pre-include the argument value you want. Then call the wrapper function with the other args when you need to.

How do you pass variable arguments in C++?

Variable number of arguments in C++Define a function with its last parameter as ellipses and the one just before the ellipses is always an int which will represent the number of arguments. Create a va_list type variable in the function definition. This type is defined in stdarg.

How do you pass variable arguments to a function in Python?

The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-key worded, variable-length argument list. The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the word args.


You can't do it directly; you have to create a function that takes a va_list:

#include <stdarg.h>

static void exampleV(int b, va_list args);

void exampleA(int a, int b, ...)    // Renamed for consistency
{
    va_list args;
    do_something(a);                // Use argument a somehow
    va_start(args, b);
    exampleV(b, args);
    va_end(args);
}

void exampleB(int b, ...)
{
    va_list args;
    va_start(args, b);
    exampleV(b, args);
    va_end(args);
}

static void exampleV(int b, va_list args)
{
    ...whatever you planned to have exampleB do...
    ...except it calls neither va_start nor va_end...
}

Maybe throwin a rock in a pond here, but it seems to work pretty OK with C++11 variadic templates:

#include <stdio.h>

template<typename... Args> void test(const char * f, Args... args) {
  printf(f, args...);
}

int main()
{
  int a = 2;
  test("%s\n", "test");
  test("%s %d %d %p\n", "second test", 2, a, &a);
}

At the very least, it works with g++.


you should create versions of these functions which take a va_list, and pass those. Look at vprintf as an example:

int vprintf ( const char * format, va_list arg );

I also wanted to wrap printf and found a helpful answer here:

How to pass variable number of arguments to printf/sprintf

I was not at all interested in performance (I'm sure this piece of code can be improved in a number of ways, feel free to do so :) ), this is for general debugprinting only so I did this:

//Helper function
std::string osprintf(const char *fmt, ...)
{
    va_list args;
    char buf[1000];
    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args );
    va_end(args);
    return buf;
}

which I then can use like this

Point2d p;

cout << osprintf("Point2d: (%3i, %3i)", p.x, p.y);
instead of for example:
cout << "Point2d: ( " << setw(3) << p.x << ", " << p.y << " )";

The c++ ostreams are beautiful in some aspects, but practically the become horrific if you want to print something like this with some small strings such as parenthesis, colons and commas inserted between the numbers.


A possible way is to use #define:

#define exampleB(int b, ...)  example(0, b, __VA_ARGS__)