何时应该编译生成移动构造函数? [英] When should compiler generate move constructor?

查看:155
本文介绍了何时应该编译生成移动构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS11并使用以下命令:

I use VS11 and use following:

class ContextWrapper
{
public:

    ContextWrapper()
    {
    } //it should be defaulted I *guess* in order to have automatic move constructor ?
      // no support in VS11 for that now  

    Context* GetContext()
    {
        return this->context.get();
    }

    void SetContext(std::unique_ptr<Context> context)
    {
        this->context = std::move(context);
    }

    //ContextWrapper(ContextWrapper&& other):  context(std::move(other.context))
    //{
    //} // I would like this to be generated by the compiler

private:
    ContextWrapper(const ContextWrapper&);
    ContextWrapper& operator= (const ContextWrapper&);

    std::unique_ptr<Context> context;
};



我希望此类生成移动构造函数/赋值。是事实,我没有一个琐碎的构造函数的原因,我不会移动?或者还有其他影响这一点的因素?

I would like this class to have generated move constructor/assignment. Is the fact that I don't have a trivial constructor the reason I don't get move ? Or there are other factors that influence this ?

推荐答案

无论标准是什么,VC11不可能实现它。所以对于今天,我不相信你能够依靠生成的移动成员。

This part of C++11 is unfortunately in flux. And whatever the standard is going to say, VC11 couldn't possibly implement it yet. So for today, I don't believe you'll be able to count on generated move members.

但是,这是一个很好的问题,我想得到一个好的

However, this is a good question and I wanted to get a good answer out on it.

一般来说,如果没有用户声明的复制成员和析构函数,编译器应该生成移动成员。 = default = delete 计为用户声明。如果你声明一个移动成员(例如移动构造函数),另一个不会隐式生成。

In general, the compiler should generate move members if you have no user-declared copy members nor destructor. = default and = delete counts as user-declared. If you declare one move member (e.g. move constructor), the other will not be implicitly generated.

不幸的是,C ++ 11继续说有时移动成员使用 = default 声明时隐式删除​​,有时它们的生成取决于基数和成员是否具有移动成员或是否可以复制。这是太复杂,有时给出令人惊讶的行为。这是跟踪此错误的CWG问题:

Unfortunately C++11 goes on to say that sometimes the move members are implicitly deleted when declared with =default, and sometimes their generation depends upon whether the bases and members have move members or are trivially copyable. This is all way too complicated and sometimes gives surprising behavior. Here is the CWG issue tracking this bug:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1402

当我写这个,这个问题没有正确的建议决议。我期望在一个星期内改变。在2012年在波特兰举行的2012年秋季C ++标准会议上,达成了一项协议,基本上说:

As I write this, the issue does not have the correct proposed resolution. I expect that to change in about a week. At the 2012 Fall C++ standards meeting in Portland, OR, an agreement was reached which basically says:


  1. 编译器不会隐式删除移动成员

  2. 移动成员​​的隐式生成将始终与 = default 相同。

  3. 隐式生成不依赖于基础或成员的微不足道,也不取决于移动时是否会抛出。

  1. The compiler will never implicitly delete move members.
  2. The implicit generation of the move members will always be the same thing as = default.
  3. Implicit generation will not depend upon the triviality of the bases or members, nor whether or not they may throw when moved.

简而言之,我希望CWG 1402的纠正措辞简单地说:

In a nutshell, I expect the corrected wording of CWG 1402 to simply say:


一般来说,如果你有no
用户声明的复制成员或析构函数。 = default = delete
计为用户声明。如果你声明一个移动成员(例如move
constructor),那么不会隐式生成另一个。
如果你 = default 一个移动成员,你会得到一个移动
每个基地和成员的东西。

In general, the compiler should generate move members if you have no user-declared copy members nor destructor. = default and = delete counts as user-declared. If you declare one move member (e.g. move constructor), the other will not be implicitly generated. And if you =default a move member, you'll get something that moves each base and member.

(正确标准化)。我还没有看到将这样说的话。 Jason Merrill正在为我们编写。

(in proper standardize). I have not yet seen the wording that will say this. Jason Merrill is writing it up for us.

这意味着有时编译器会隐式生成抛出移动成员。但我们正在制定简单的规则,尽管在大多数时候都做了正确的事情(很少有惊喜)。

This will mean that sometimes the compiler will implicitly generate throwing move members. But we were going for simple rules that nevertheless did the right thing most of the time (few surprises).

这篇关于何时应该编译生成移动构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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