shasan
shasan

Reputation: 188

Can a variable function arguments list be passed by reference?

We can create functions with variable number of arguments using the ... notation in stdarg library. Is it also possible to pass these arguments by reference? E.g. perhaps to return a variable number of output values.

Upvotes: 1

Views: 63

Answers (2)

Not really (except with template varargs), for variadic functions in the sense of <stdarg.h>. You cannot call

void foo(int, ...);

as

int x = 0, y = 1;
foo(2,x,y);

and expect the x and y to be passed by reference. However, you can pass their address:

foo(2, &x, &y);

And you probably could play variadic template tricks or preprocessor macros tricks to transform FOO(x,y) into foo(2,&x,&y)

Upvotes: 3

Bill Lynch
Bill Lynch

Reputation: 81926

You can't with stdarg.h, but if you use c++ features you could.

I'd recommend looking into the implementation of std::tie as an example of how the standard library does this.

Upvotes: 3

Related Questions