简体   繁体   English

如果将结构传递给vararg函数会怎样?

[英]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.

相关问题 将给定的 vararg 参数传递给另一个函数的合适方法是什么? - What is the appropriate way to pass given vararg parameter to another function? 当我清除一个结构向量时会发生什么? - What happens when I clear a vector of struct? struct /中的重载函数调用operator()对参数会发生什么? - Overload function call operator () in struct / what happens to arguments? 当我修改复制构造函数以进行一些奇怪的操作,然后按值将(该类的)对象传递给函数时,会发生什么? - What happens when I modify the copy constructor to do something weird and then pass an object (of that class) by value to a function? 如果将结构体放入结构体中会发生什么? - What happens if you put a struct within a struct? 当我在DLL中调用函数时会发生什么 - What happens when I call a function in a DLL 强制转换const以将其传递给具有引用功能的函数,会发生什么? - casting const to pass it to function that takes reference, what happens? 当我们将类类型的参数传递给 cpp 中的函数时会发生什么? - What happens when we pass an argument of class type to a function in cpp? 如果我传递一个临时引用并将其存储为 class 成员会怎样? - What happens if I pass a temporary reference and store it as a class member? 如果我初始化一个非 0 的虚拟 function 会发生什么? 它会创建一个纯虚拟 function 还是 memory 中会发生什么? - what happens if I initialize a virtual function other than 0? will it create a pure virtual function or what else happens in memory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM