PEC*_*PEC 2 c++ in-place copy-elision c++17
我试图让复制省略来处理要返回的对象的字段。
示例代码:
#include <iostream>
struct A {
bool x;
A(bool x) : x(x) {
std::cout << "A constructed" << std::endl;
}
A(const A &other) : x(other.x) {
std::cout << "A copied" << std::endl;
}
A(A &&other) : x(other.x) {
std::cout << "A moved" << std::endl;
}
A &operator=(const A &other) {
std::cout << "A reassigned" << std::endl;
if (this != &other) {
x = other.x;
}
return *this;
}
};
struct B {
A a;
B(const A &a) : a(a) {
std::cout << "B constructed" << std::endl;
}
B(const B &other) : a(other.a) {
std::cout << "B copied" << std::endl;
}
B(B &&other) : a(other.a) {
std::cout << "B moved" << std::endl;
}
B &operator=(const B &other) {
std::cout << "B reassigned" << std::endl;
if (this != &other) {
a = other.a;
}
return *this;
}
};
B foo() {
return B{A{true}};
}
int main() {
B b = foo();
std::cout << b.a.x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我编译:
g++ -std=c++17 test.cpp -o test.exe
输出:
A constructed
A copied
B constructed
1
Run Code Online (Sandbox Code Playgroud)
B 就地构建。为什么A不是?我至少希望它是移动构造的,但它被复制了。
有没有办法在要返回的 B 内部就地构造 A?如何?
B从 an构造 aA涉及复制A- 它在您的代码中是这样说的。这与函数返回中的复制省略无关,所有这些都发生在B. 标准中的任何内容都不允许在成员初始化列表中省略(如在“打破 as-if 规则”中)复制构造。查看[class.copy.elision]可能违反 as-if 规则的少数情况。
换句话说:创建B b{A{true}};. 函数返回完全一样好,但不是更好。
如果要A移动而不是复制,则需要一个构造函数B(A&&)(然后移动构造a成员)。
| 归档时间: |
|
| 查看次数: |
163 次 |
| 最近记录: |