未在函数结果上调用复制构造函数

发布于 2024-12-02 06:14:51 字数 1084 浏览 0 评论 0原文

GCC 内嵌了一个声明——无论我如何努力阻止它。我尝试了

  • -fno-inline
  • -O0
  • __attribute__ ((noinline))
  • dummy asm("")

没有成功! 这里是代码:

#include<iostream>

using namespace std;

struct A {
  A() {cout << "A::A()" <<endl; }
  A(const A& a) {cout << "A::A(copy)" <<endl; }
  A& operator=(const A& a) {cout << "A::=()" <<endl; return *this;}
};

A __attribute__ ((noinline)) func() 
{
  cout << "func()" << endl;
  A loc;
  asm("");
  return loc;
}

int main() {
  A a = func();
}

(g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2) 的不幸输出是

func()
A::A()

语句 A a = func(); 发生了什么; ??

这个实验的原因是我想知道当执行到这个语句时会发生什么(因为我需要控制如何完成它):

A a = func();

我读到当一个人这样做时会调用复制构造函数

A a = b;

(在这种情况下复制构造函数但在 A a = func(); 的情况下不会被调用。 该函数是内联的。我需要控制这个语句,因为我的“struct A” 现实生活中包含需要处理的动态分配的数据。

我在这里错过了一些明显的东西吗?

GCC inlines a statement -- no matter how hard I try to prevent it. I tried

  • -fno-inline
  • -O0
  • __attribute__ ((noinline))
  • dummy asm("")

No success!
Here the code:

#include<iostream>

using namespace std;

struct A {
  A() {cout << "A::A()" <<endl; }
  A(const A& a) {cout << "A::A(copy)" <<endl; }
  A& operator=(const A& a) {cout << "A::=()" <<endl; return *this;}
};

A __attribute__ ((noinline)) func() 
{
  cout << "func()" << endl;
  A loc;
  asm("");
  return loc;
}

int main() {
  A a = func();
}

The unfortunate output of this (g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2) is

func()
A::A()

What happened to the statement A a = func(); ??

The reason for this experiment is that I would like to know what happens when execution comes to this statement (because I need control how this is done):

A a = func();

I read that the copy constructor is called when one does

A a = b;

(In this case the copy-constructor is called. But not in the case A a = func();)
The function is inlined instead. I NEED control over this statement since my "struct A"
in real life contains dynamically allocated data that needs to be taken care of.

Am I missing something obvious here ?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天赋异禀 2024-12-09 06:14:51

不,这与内联函数无关。内联函数不会改变可观察的行为。

这是一种称为复制省略的优化,它允许编译器通过直接在目标构造返回值来避免复制。您可以使用 g++ 标志 -fno-elide-constructors 禁用它。

无论如何,动态分配的数据应该不成问题。假设有一个健全的复制构造函数,您将看到的唯一区别可能是更好的性能。

No, this has nothing to do with the function being inlined. Inlining the function would not change observable behaviour.

This is an optimization called copy elision that allows the compiler to avoid a copy by constructing the return value directly at the destination. You can disable it with the g++ flag -fno-elide-constructors.

In any case, the dynamically allocated data should not be a problem. Assuming a sane copy constructor, the only difference you will see will be possibly better performance.

笨死的猪 2024-12-09 06:14:51

如果 struct A 包含动态分配的数据,那么您有责任在适当的析构函数/构造函数中管理该内存。许多类管理动态分配的数据并且可以很好地处理省略副本。 RVO 和 NRVO 是重要的优化。

If struct A contains dynamically allocated data, then it's your responsibility to manage that memory in the appropriate destructor/constructor. Many classes manage dynamically allocated data and work just fine with ellided copies. RVO and NRVO are important optimizations.

漫雪独思 2024-12-09 06:14:51

如果有人(像我一样)真的想避免内联:

-fkeep-inline-functions -fno-inline

-fkeep-内联函数
即使对给定函数的所有调用都已集成,并且该函数被声明为静态,仍然会输出该函数的单独运行时可调用版本。此开关不影响外部内联函数。

-fno-内联
不要注意 inline 关键字。通常,此选项用于防止编译器扩展任何内联函数。请注意,如果您不进行优化,则无法内联扩展任何函数。

In case anyone (like me) is really looking for avoiding inline:

-fkeep-inline-functions -fno-inline

-fkeep-inline-functions
Even if all calls to a given function are integrated, and the function is declared static, nevertheless output a separate run-time callable version of the function. This switch does not affect extern inline functions.

-fno-inline
Don't pay attention to the inline keyword. Normally this option is used to keep the compiler from expanding any functions inline. Note that if you are not optimizing, no functions can be expanded inline.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文