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? Eg perhaps to return a variable number of output values.
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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.