本文介绍了c ++变量参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索答案,但我找不到任何相关信息。让我们举个例子:

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);
}

参数 a b c 通过?按值,或通过引用和如何使用va_arg请求它们? MyClass的构造函数/析构函数怎么样?

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?

推荐答案

不应该在var-arg函数中使用用户定义的类型。使用C ++ 11可变参数模板。

You should never use user-defined types in var-arg function. Use C++11 variadic templates.

如果你的类不是pod类型 - 它没有标准规定,感谢Vaughn Cato的意见

If your class is not pod-type - it's unspecified by standard, thanks to Vaughn Cato for remark

n3337 5.2.2 / 7


传递具有非重要
拷贝构造函数的类类型的潜在求值参数(第9章) ,非平凡移动构造函数或非平凡析构函数,没有对应的
参数,由实现定义的语义有条件地支持。

n3337 5.2.2/7

Passing a potentially-evaluated argument of class type (Clause 9) having a nontrivialcopy constructor, a non-trivial move constructor, or a non-trivial destructor, with no correspondingparameter, is conditionally-supported with implementation-defined semantics.

否则,你可以,这是正确的,但你不会。

Else, you can and it will be correct, but you shouln't.

这篇关于c ++变量参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 09:46