[英]What is the appropriate way to pass given vararg parameter to another function?
[英]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?
}
是否將整個struct
復制並傳遞到堆棧上? 如果是,則調用復制構造函數嗎? 指針通過了嗎? 這樣安全嗎?
是的,如果您傳遞左值,則將完成左值到右值的轉換,這意味着調用復制構造函數將對象復制到新副本中並將其作為參數傳遞。
void f(...)
使用按位復制。 因為somestruct
只有C ++內置類型,所以不會為您的somestruct
生成默認的構造函數或復制構造函數。
Is this safe?
是的,這是絕對安全的。
我將向您介紹“內部C ++對象模型” chapter 2 The Semantics of Constructors
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.