[英]Is it necessary to implement move constructor for a class without dynamic resources?
[英]Is move constructor necessary on (POD) structs?
在C ++ 11标准的12.8 / 15段中定义:
非联合类X的隐式定义的复制/移动构造函数执行其基和成员的成员复制/移动。 [...]
这是否意味着:
struct st {
int a;
int b;
// ...
};
// ...
void do_smt(st tmp) {
st lala(std::move(tmp));
// ...
}
// ...
int main(int argc, char* argv[]) {
st test(1, 2);
do_smt(std::move(test));
}
没有move构造函数将可以工作吗?
是的,它无需指定move构造函数即可工作; 在您的示例中,编译器可能会通过初始化lala而不是test来优化移动操作; 检查有关汇编程序的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.