- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我看到了这个问题 When is an object "out of scope"?
我看了sparc_spread 的回答,发现其中有一个问题。在他的回答的这一部分:
Circle myFunc () {
Circle c (20);
return c;
}
// The original c went out of scope.
// But, the object was copied back to another
// scope (the previous stack frame) as a return value.
// No destructor was called.
他说过“没有调用析构函数。”但是当我尝试运行此代码(由我编写)时:
/* Line number 1 */ #include <iostream>
/* Line number 2 */ #include <string>
/* Line number 3 */ using namespace std;
/* Line number 4 */ class test {
/* Line number 5 */ public:
/* Line number 6 */ test(int p) {
/* Line number 7 */ cout << "The constructor ( test(int p) ) was called"<<endl;
/* Line number 8 */ }
/* Line number 9 */ test(test&&c)noexcept {
/* Line number 10 */ cout << "The constructor ( test(test && c) ) was called" << endl;
/* Line number 11 */ }
/* Line number 12 */ ~test() {
/* Line number 13 */ cout << "The distructor was called" << endl;
/* Line number 14 */ }
/* Line number 15 */ };
/* Line number 16 */ test function() {
/* Line number 17 */ test i(8);
/* Line number 18 */ return i;
/* Line number 19 */ }
/* Line number 20 */ int main()
/* Line number 21 */ {
/* Line number 22 */ test o=function();
/* Line number 23 */ return 0;
/* Line number 24 */ }
输出:
The constructor ( test(int p) ) was called
The constructor ( test(test && c) ) was called
The distructor was called
The distructor was called
所以我的代码的输出显示:
调用了两个构造函数(这不是我要讨论的重点。所以我不会讨论(为什么、何时或如何)调用了两个构造函数?)
调用了两个析构函数
当我使用调试器(知道第一个析构函数何时被调用)时,我发现第一个析构函数在第 18 行被调用(我代码中的第 18 行)。
最后。我的观点对吗?
最佳答案
Does returning a local variable return a copy and destroy the original?
您问题的最终答案是,这取决于是否启用了优化。因此,让我们分别讨论每个案例。另请注意,由于原始问题中的给定输出是针对 C++17 的,因此下面的讨论也适用于相同的(C++17 及以上版本)。
在这里我们将看到启用优化 (NRVO) 时会发生什么。
class test {
public:
test(int p) {
cout << "The constructor ( test(int p) ) was called: "<<this<<endl;
}
test(test&&c)noexcept {
cout << "The constructor ( test(test && c) ) was called: "<<this << endl;
}
~test() {
cout << "The distructor was called: "<<this << endl;
}
};
test function() {
test i(8);
return i;
}
int main()
{
test o=function();
return 0;
}
output该程序是(启用 NRVO):
The constructor ( test(int p) ) was called: 0x7fff78e42887 <-----object o construction
The distructor was called: 0x7fff78e42887 <-----object o destruction
可以使用称为命名返回值优化(aka NRVO)的优化来理解上述输出,如 copy elison 中所述。其中指出:
Under the following circumstances, the compilers are permitted, but not required to omit the copy and move (since C++11) construction of class objects even if the copy/move (since C++11) constructor and the destructor have observable side-effects. The objects are constructed directly into the storage where they would otherwise be copied/moved to. This is an optimization: even when it takes place and the copy/move (since C++11) constructor is not called, it still must be present and accessible (as if no optimization happened at all), otherwise the program is ill-formed:
- In a return statement, when the operand is the name of a non-volatile object with automatic storage duration, which isn't a function parameter or a catch clause parameter, and which is of the same class type (ignoring cv-qualification) as the function return type. This variant of copy elision is known as NRVO, "named return value optimization".
(强调我的)
让我们将其应用于上面给出的示例并尝试理解输出。名为 i
的变量是一个局部变量,意味着它具有自动存储持续时间,因此根据上面引用的语句,允许编译器(但不是必需的!)直接将对象构造到变量的存储中名为 o
。也就是说,它好像你wrote :
test o(5); //equivalent to this due to NRVO
因此在这里我们首先看到对对象 o
的转换构造函数 test::test(int)
的调用,然后是对该对象 o 的析构函数调用
。
您可以选择使用 -fno-elide-constructors
标志禁用此优化。当使用这个标志执行相同的程序时,output该计划将成为:
The constructor ( test(int p) ) was called: 0x7ffda9d94fe7 <-----object i construction
The constructor ( test(test && c) ) was called: 0x7ffda9d95007 <-----object o construction
The distructor was called: 0x7ffda9d94fe7 <-----object i destruction
The distructor was called: 0x7ffda9d95007 <-----object o destruction
这一次,因为我们向编译器提供了 -fno-elide-constructors
标志,所以 NRVO 被禁用。这意味着现在编译器不能省略 return 语句 return i;
对应的复制/移动构造。这反过来意味着首先对象 i
将使用转换构造函数 test::test(int)
构造,因此我们在输出中看到第一行。
接下来,这个名为 i
的局部变量将使用移动构造函数 test::test(test&&)
移动,因此我们看到输出的第二行。请注意,由于强制复制elison,对象o
将直接从这个移动的prvalue 直接构建,因为您使用的是C++17。
接下来,局部变量 i
将使用析构函数 test::~test()
析构,我们在输出中看到第三行。
最后,对象 o
将被销毁,我们看到输出的第四行。
在这种情况下,它是好像你wrote :
test o = std::move(test(5)); //equivalent to this
关于c++ - 返回局部变量是否返回拷贝并销毁原始变量(nrvo)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72383114/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!