在c++11中构造

Construction in c++11

本文关键字:c++11      更新时间:2023-10-16

我正在努力学习更多关于c++的知识,但我对编译器的工作有点困惑。我写了以下文件,其中详细说明了发生的事情:

Test getTest()
{
return Test(100, string("testing..."));
}
int main()
{
// These two will call the initializer constructor...
Test t1(5, string("hello"));
Test t2(10, string("goodbye"));
// This will not call operator=. This will call the copy constructor!
Test t3 = t1;
// This will call operator=(Test&)
t3 = t2;
// This will call operator=(Test&&) because rhs is an rvalue
//   We will swap the resources in this operator= so that when getTest()
//   deletes its resources, it will actually be deleting t3's old resources.
//   Likewise, t3 will get getTest()'s resources.
t3 = getTest();

// I don't know what this is doing, but I know it's not calling the destructor.
//   I beleive that the memory of t4 is simply what was returned by getTest().
// Likewise with t5.
Test t4(getTest());
Test* t5 = new Test(getTest());
Test t6(t4);
return 0;
}

看起来t4和t5没有输入任何构造函数,实际上只是使用getTest()分配的内存。我认为t4会进入rValue复制构造函数:Test(const Test&&rhs),但即使它的参数是rValue,它也不会。测试t4(getTest())不调用任何析构函数,这就是为什么我认为t4只是获取内存。t6确实调用了复制构造函数。

我在Visual Studio 2013中查看了汇编代码,发现了以下内容:

Test t4(getTest());
00F59B8C  push        8  
00F59B8E  lea         ecx,[t4]  
00F59B91  call        Test::__autoclassinit2 (0F51285h)  
00F59B96  lea         eax,[t4]  
00F59B99  push        eax  
00F59B9A  call        getTest (0F51456h)  
00F59B9F  add         esp,4  
00F59BA2  mov         byte ptr [ebp-4],8  

所以它看起来像是调用了一个叫做autoclassinit2的东西,然后从getTest中获取内存,最后将其存储在t4中?

所以我想我的问题是:这只是一个编译器优化,直接将getTest()中构造函数的内存提供给t4吗?而不是说,1。在getTest()2中构造。正在调用rVal复制构造函数3。正在破坏getTest()内存?还是这里发生了其他事情?谢谢

=在声明中的意思是"请隐式构造"(基本上),而不是"调用运算符="。

C++有一个被称为省略的概念。Elision的意思是"使两个或多个变量成为同一事物"。编译器什么时候可以这样做是有规则的。与其他优化不同,即使有副作用,它也是合法的。

如果使用相同类型的未命名临时变量初始化变量,编译器可以删除该临时变量。如果从函数返回局部变量,则可以(在某些情况下)将其隐藏到未命名的返回值中。未命名的临时物品归还也是如此。NRVO和RVO是您想要的关键字。

在许多情况下,如果省略由于技术原因或编译器限制而失败,则会隐式发生move。然而,显式move阻止省略。因此,了解这些规则对于优化代码来说非常重要。