在c++11中构造
Construction in c++11
我正在努力学习更多关于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
阻止省略。因此,了解这些规则对于优化代码来说非常重要。
- MSVC是否支持C++11样式的属性而不是__declspec
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 如何将模板转换为C++11之前的模板
- c++11评估顺序(未定义的行为)
- C++中的VLA,扩展名为std=C++11
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- "类模板示例<int>;"语句对 C++11 是什么意思?
- this_thread::sleep_for和计时时钟之间的关系是否由C++11标准指定
- 如何使用lock_guard在c++11中实现scoped_lock功能
- C++11 中不同类型的对象的 std::array 的替代方案
- 为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?
- 为什么我的C++代码中出现'Segmentation Fault: 11'行?
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- STLPort using C++11
- Qt 5.11.2 (Clang 8.0 (Apple), 64 位), 找不到 QJSEngine 文件
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- C++11 迭代向量的新方法?