rvalue hello world missing构造函数 [英] rvalue hello world missing constructor
问题描述
我想知道更多关于右值引用,但我陷入了这个最简单的例子:
I'm trying to know more about rvalue references but I got stuck on this simplest example:
#include <iostream>
using namespace std;
struct C {
C() { cout << "C()\n"; }
~C() { cout << "~C()\n"; }
C(const C&) { cout << "C(const C&)\n"; }
C& operator=(const C&) { cout << "operator=(const C&)\n"; return *this; }
C(C&&) { cout << "C(C&&)\n"; }
C& operator=(C&&) { cout << "operator=(C&&)\n"; return *this; }
};
C foo() { C c; return c; }
int main()
{
const C c = foo();
return 0;
}
我已经使用Clang 3.2和 std = c ++ 11 -fno-elide-constructors
(避免(N)RVO),但结果令我惊讶:
I've compiled it with Clang 3.2 and -std=c++11 -fno-elide-constructors
(to avoid (N)RVO) but the result is surprising to me:
C()
~C() // huh?
C(C&&)
~C()
~C()
b $ b
我期望除了第一个〜C()
。它从哪里来,什么是我失踪,因为有2个建筑和3破坏?是&&构造函数用一个销毁对象引用调用
I expected exactly that except for the first ~C()
. Where did it came from and what am I missing because there are 2 constructions and 3 destructions? Is the && constructor called with a destroyed object reference??
推荐答案
这必须是一个错误。在 foo()
中构造的局部对象的析构函数在接收对象的move构造函数之前被调用。特别地,当按值返回时,似乎分配了一个临时,但不构造(move-)。以下程序显示了这一点:
This must be a bug. The destructor for the local object constructed in foo()
is being invoked before the move constructor of the receiving object. In particular, it seems a temporary is allocated but not (move-)constructed when returning by value. The following program shows this:
#include <iostream>
using namespace std;
struct C
{
C(int z) { id = z; cout << "C():" << id << endl; }
~C() { cout << "~C():" << id << endl; }
C(const C& c) { id = c.id + 1; cout << "C(const C&):" << id << endl; }
C& operator=(const C&) { cout << "operator=(const C&)\n"; return *this; }
C(C&& c) { id = c.id + 1; cout << "C(C&&):" << id << endl;}
C& operator=(C&&) { cout << "operator=(C&&)\n"; return *this; }
int id;
};
C foo() { C c(10); return c; }
int main()
{
const C c = foo();
return 0;
}
输出:
C():10
// THE TEMPORARY OBJECT IS PROBABLY ALLOCATED BUT *NOT CONSTRUCTED* HERE...
~C():10 // DESTRUCTOR CALLED BEFORE ANY OTHER OBJECT IS CONSTRUCTED!
C(C&&):4198993
~C():4198992
~C():4198993
在 foo()
中创建两个对象似乎可以解决这个问题:
Creating two objects inside of foo()
seems to shed some more light on the issue:
C foo() { C c(10); C d(14); return c; }
输出:
C():10
C():14
~C():14
// HERE, THE CONSTRUCTOR OF THE TEMPORARY SHOULD BE INVOKED!
~C():10
C(C&&):1 // THE OBJECT IN main() IS CONSTRUCTED FROM A NON-CONSTRUCTED TEMPORARY
~C():0 // THE NON-CONSTRUCTED TEMPORARY IS BEING DESTROYED HERE
~C():1
,这似乎取决于如何在 foo()
中构造对象。如果 foo()
是这样写的:
Interestingly, this seems to depend on how the object is constructed in foo()
. If foo()
is written this way:
C foo() { C c(10); return c; }
然后出现错误。如果它是这样写的,它不会:
Then the error appears. If it is written this way it does not:
C foo() { return C(10); }
输出 foo()的最后一个定义
Output with this last definition of foo()
:
C():10 // CONSTRUCTION OF LOCAL OBJECT
C(C&&):11 // CONSTRUCTION OF TEMPORARY
~C():10 // DESTRUCTION OF LOCAL OBJECT
C(C&&):12 // CONSTRUCTION OF RECEIVING OBJECT
~C():11 // DESTRUCTION OF TEMPORARY
~C():12 // DESTRUCTION OF RECEIVING OBJECT
这篇关于rvalue hello world missing构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!