问题
I'm searching for answers but i can't find any relevant information on this. Let's take the example:
class MyClass
{
//member functions and variables
};
void foo(int pivot,...)
{
va_list arguments;
va_start(arguments,pivot);
//va_arg(arguments,???)
va_end(arguments);
}
void bar()
{
MyClass a;
MyClass * b = &a;
const MyClass & c = a;
foo(0,a,b,c);
}
How are the arguments a
,b
and c
passed? By value , or by reference and how to ask for them using va_arg? What about the constructors/destructor for MyClass? Where in the c++ standard is this kind of behavior specified?
回答1:
You should never use user-defined types in var-arg function. Use C++11 variadic templates.
If your class is not pod-type - it's unspecified by standard, thanks to Vaughn Cato for remark
n3337 5.2.2/7
Passing a potentially-evaluated argument of class type (Clause 9) having a nontrivial copy constructor, a non-trivial move constructor, or a non-trivial destructor, with no corresponding parameter, is conditionally-supported with implementation-defined semantics.
Else, you can and it will be correct, but you shouln't.
回答2:
By value. But beware, if MyClass
is not a POD, the program
has undefined behavior (C++03, §5.2.2/7), or if MyClass
has
a non-trivial copy constructor, move constructor or destructor,
the operation is conditionally supported, with implementation
defined semantics (C++11, §5.2.2/7).
In your example, passing a
and passing c
are exactly
identical operations (except that c
cannot be bound to
a non-const reference, but that's not an issue here, since
varargs are all pass by value). Thus, when calling foo
, you
pass 0
, a copy of a
, a copy of the pointer b
, and a copy
of a
. In order to access them in foo
, you need to declare
the types in va_arg
as int
, MyClass
, MyClass*
and
MyClass
.
来源:https://stackoverflow.com/questions/15026671/c-variable-argument-list