[英]what happens if I pass a struct to a vararg function?
void f(int count, ...){
//whatever
}
struct somestruct{
size_t a, b, c;
};
int main() {
somestruct s;
f(1, s); //what is actually passed?
}
Is the entire struct
copied and passed on the stack? 是否将整个
struct
复制并传递到堆栈上? If so are copy constructors called? 如果是,则调用复制构造函数吗? Is the pointer passed?
指针通过了吗? Is this safe?
这样安全吗?
是的,如果您传递左值,则将完成左值到右值的转换,这意味着调用复制构造函数将对象复制到新副本中并将其作为参数传递。
void f(...)
is using bit-wised copy. void f(...)
使用按位复制。 No default constructor or copy constructor will be generated for your somestruct
as it only has C++ build-in types. 因为
somestruct
只有C ++内置类型,所以不会为您的somestruct
生成默认的构造函数或复制构造函数。
Is this safe?
Yes, this is perfectly safe. 是的,这是绝对安全的。
I'll refer you to 'Inside C++ Object Model' chapter 2 The Semantics of Constructors
我将向您介绍“内部C ++对象模型”
chapter 2 The Semantics of Constructors
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.