显式move构造函数 [英] Explicit move constructor

查看:115
本文介绍了显式move构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编译以下代码:

struct Foo
{
    explicit Foo ( void ) { }
    explicit Foo ( Foo&& rhs ) { }
};

Foo bar ( void )
{
    return Foo();
}

出现以下错误:


调用隐式删除的'Foo'复制构造函数

call to implicitly-deleted copy constructor of 'Foo'

这很明显

问题1:为什么编译器需要的copy-ctor? ?我期望 bar 的返回值将通过带有move-ctor的右值 Foo()构造。

Question 1: Why does the compiler need the copy-ctor of Foo? I expected the return value of bar to be constructed from the rvalue Foo() with move-ctor.

然后我将move-ctor重新声明为隐式,并且一切都成功编译。

Then I redeclare the move-ctor as implicit and everything compiles successfully.

问题2:为什么当我重新将move-ctor声明为隐式时,编译器不再需要copy-ctor?

Question 2: Why the compiler does not need copy-ctor any more when I redeclare the move-ctor as implicit?

问题3: explicit 关键字表示在复制和移动ctor的上下文中,因为它的含义肯定与常规ctor的上下文不同。

Question 3: What does explicit keyword mean in the context of copy and move ctors as it definitely means something different from from the context of regular ctors.

推荐答案

这是因为返回值被视为隐式转换

It is because returning a value is considered an implicit conversion.

引用C ++ 11标准:

Quoting from the C++11 standard:


6.6.3返回语句


2 [...]

可以使用带有非无效类型表达式的return语句仅在功能上ns返回值;表达式的值返回给函数的调用者。 表达式的值隐式转换为它所出现的函数的返回类型。 return语句可能涉及临时对象的构造和复制或移动(12.2)。 [...]

A return statement with an expression of non-void type can be used only in functions returning a value; the value of the expression is returned to the caller of the function. The value of the expression is implicitly converted to the return type of the function in which it appears. A return statement can involve the construction and copy or move of a temporary object (12.2). [...]

从返回表达式到保留返回值的临时对象的转换是隐式的。就像会导致错误一样

The conversion from the return expression to the temporary object to hold the return value is implicit. So just as this would result in an error

Foo f = Foo();   // Copy initialization, which means implicit conversion

具有示例代码也将触发类似的代码。

having code as your example would also trigger a similar one.


问题1:为什么编译器需要Foo的copy-ctor?我期望bar的返回值将由带有move-ctor的右值Foo()构造。

Question 1: Why does the compiler need the copy-ctor of Foo? I expected the return value of bar to be constructed from the rvalue Foo() with move-ctor.

这是因为 Foo (Foo&&)由于上述原因,不是可行的重载。规则指出,每当无法使用move构造函数时,即是编译器应考虑复制构造函数的情况,在您的情况下,由于存在用户定义的move构造函数而将其隐式删除。

It is because Foo(Foo&&) is not a viable overload, because of the reasons above. The rules state that whenever the move constructor cannot be used, it is when the compiler shall then consider the copy constructor, which, in your case, is implicitly deleted due to the presence of a user-defined move constructor.


问题2:为什么当我将move-ctor声明为隐式时,编译器不再需要copy-ctor了?

Question 2: Why the compiler does not need copy-ctor any more when I redeclare the move-ctor as implicit?

这是因为您的move构造函数现在可以使用了。因此,编译器甚至可以在不考虑复制构造函数的情况下立即使用它。

It is because your move constructor can be used now. Thus, the compiler can immediately use it without even considering the presence of the copy constructor.


问题3:显式关键字在copy和

Question 3: What does explicit keyword mean in the context of copy and move ctors as it definitely means something different from from the context of regular ctors.

恕我直言,这毫无意义,只会导致问题,就像您的情况一样。

IMHO, it doesn't make sense, and it only leads to problems, just as in your case.

这篇关于显式move构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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