c++ - 我可以将 std::move 与不提供 move 构造函数的类一起使用吗?

标签 c++ c++11 move move-semantics

我有这样一个类:

class myClass
{
    int x[1000];
 public:
     int &getx(int i)
    {
        return x[i];
    }
}

请注意,我没有在这里提供 move 结构。

如果我使用下面的代码:

myClass A;
auto B=std::move(a);

是 A move 到 B 还是由于我没有提供 move 构造函数,A 被复制到 B?

对象是否有任何默认的 move 构造函数?如果是,它如何处理指针和动态分配的数组?

最佳答案

虽然您没有提供显式 move 构造函数,但编译器为您提供了隐式 move 构造函数。

If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if

  • X does not have a user-declared copy constructor, and
  • X does not have a user-declared copy assignment operator,
  • X does not have a user-declared move assignment operator,
  • X does not have a user-declared destructor, and
  • the move constructor would not be implicitly defined as deleted.

所以你的问题的答案是:不,A 没有被复制到 B。

不清楚你在其他问题中问的是什么。请指定更多详细信息。

关于c++ - 我可以将 std::move 与不提供 move 构造函数的类一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50609626/

相关文章:

c# - C++如何实现list的数组

c++ - 我可以在 void 函数中返回吗?

c++ - 本地结构中是否允许成员声明 `decltype(name) name;`,其中第一个名称指的是封闭范围?

Javascript: move 以编程方式创建的图像不起作用

android - 上下 move ImageView 的动画

c++ - 填充 unordered_set 的更有效方法?

c++ - 为什么允许我声明一个带有已删除析构函数的对象?

c++ - std::array 类成员在编译时设置?

c++ - 为什么 c++11 随机分布是可变的?

git - 如何将本地 git 存储库从父文件夹 move 到子文件夹?