(POD)结构上是否需要移动构造函数

Is move constructor necessary on (POD) structs?

本文关键字:移动 构造函数 是否 POD 结构上      更新时间:2023-10-16

在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而不是测试来优化move操作;请检查汇编程序的输出。