为什么没有默认的移动赋值/移动构造函数? [英] Why no default move-assignment/move-constructor?

查看:922
本文介绍了为什么没有默认的移动赋值/移动构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个简单的程序员。我的类成员变量最常由POD类型和STL容器组成。因为这个我很少写分配操作符或复制构造函数,因为这些是默认实现。

I'm a simple programmer. My class members variables most often consists of POD-types and STL-containers. Because of this I seldom have to write assignment operators or copy constructors, as these are implemented by default.

如果我使用 std :: move 在不可移动的对象上,它使用赋值运算符,意味着 std :: move 是完全安全的。

Add to this, if I use std::move on objects not movable, it utilizes the assignment-operator, meaning std::move is perfectly safe.

由于我是一个简单的程序员,我想利用移动功能,而无需为每个类添加移动构造函数/赋值运算符,因为编译器可以简单地实现它们as this-> member1_ = std :: move(other.member1 _); ...

As I'm a simple programmer, I'd like to take advantage of the move-capabilities without adding a move constructor/assignment operator to every class I write, as the compiler could simply implemented them as "this->member1_ = std::move(other.member1_);..."

但是它没有(至少在Visual 2010中没有),有什么特别的原因吗?

But it doesn't (at least not in Visual 2010), is there any particular reason for this?

更重要的是, 有任何方法解决此问题吗?

更新: b如果你低头看GManNickG的答案,他提供了一个伟大的宏。如果你不知道,如果你实现移动语义,你可以删除交换成员函数。

Update: If you look down at GManNickG's answer he provides a great macro for this. And if you didn't know, if you implement move-semantics you can remove the swap member function.

推荐答案

隐式生成的移动构造函数和赋值运算符一直有争议,并且在最近的C ++标准草案中有重大修订,因此目前可用的编译器在隐式生成方面可能会有不同的行为。

The implicit generation of move constructors and assignment operators has been contentious and there have been major revisions in recent drafts of the C++ Standard, so currently available compilers will likely behave differently with respect to implicit generation.

有关问题的详情,请参阅 2010 WG21文章列表< a>并搜索mov

For more about the history of the issue, see the 2010 WG21 papers list and search for "mov"

当前规范(N3225,从11月起)表示(N3225 12.8 / 8):

The current specification (N3225, from November) states (N3225 12.8/8):


如果类 X 的定义没有显式地声明一个移动构造函数,那么将隐式声明为如果

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 没有用户声明的复制构造函数,和

  • X does not have a user-declared copy constructor, and

X 没有用户声明的副本分配运算符

X does not have a user-declared copy assignment operator,

X 没有用户声明的移动赋值运算符,

X does not have a user-declared move assignment operator,

X 没有用户声明的析构函数,

X does not have a user-declared destructor, and

不会被隐式定义为已删除。

the move constructor would not be implicitly defined as deleted.

12.8 / 22中有类似的语言,隐式声明为默认值。您可以在 N3203:紧缩生成隐式移动的条件
,这主要基于Bjarne Stroustrup的论文中提出的一个解决方案 N3201:沿着右移。

There is similar language in 12.8/22 specifying when the move assignment operator is implicitly declared as defaulted. You can find the complete list of changes made to support the current specification of implicit move generation in N3203: Tightening the conditions for generating implicit moves , which was based largely on one of the resolutions proposed by Bjarne Stroustrup's paper N3201: Moving right along.

这篇关于为什么没有默认的移动赋值/移动构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆