-O0或其他优化级别中包含-fno-elide-constructors? [英] Is -fno-elide-constructors contained in -O0 or any other optimization level?

查看:680
本文介绍了-O0或其他优化级别中包含-fno-elide-constructors?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-O0或其他-O [其他级别]中包含-fno-elide-constructors吗?



在我看来,-fno-elide-constructors是优化标志,-O是优化级别。因此,一些-O级别可能包括flag -fno-elide构造函数。
我是对的吗?

换句话说,-fno-elide-constructors和-O ..之间是否有任何关系?

解决方案


换言之,-fno-elide-constructors
和-O ..之间是否存在关系?

是的,有一个关系,虽然很简单:gcc很可能已经在 -O0 级别,除非您明确禁用它。

尽管编译器可以做很多令人讨厌的事情,但请参阅 RVO强制编译失败时出错。底线是:你总是需要检查生成的程序集,看看底下真正发生了什么。



请记住( from Working Draft,Standard for Programming
Language C ++,N3797 < a>这是我能找到的C ++ 14最接近的近似值):


12.8 / 31当满足某些标准时,实现允许省略类对象的复制/移动构造,即使为复制/移动操作选择的构造函数和/或对象的析构函数具有副作用。 [b]






证明我声明的代码:

  #include  constexpr int size = 1024; 

struct A {int array [size] = {0}; };

int main(){
A a = A();
std :: printf(%d \ n,a.array [size-1]);

$ / code>






g ++ -std = c ++ 11 -Wall -O0 -S elide.cpp ,在生成的汇编代码中只有一个

  call A :: A()

然而, g ++ -std = c ++ 11 -Wall -O0 -fno-elide-constructors -S elide.cpp 我得到:

 呼叫A :: A()
[...]
呼叫A :: A(A&& code>

即使您使用 -O0 禁用优化,仍然必须禁用另外,如果您因为某些原因需要禁用该功能,则可以使用该功能。


Is -fno-elide-constructors contained in -O0 or any other -O[other level]?

In my view, -fno-elide-constructors is an optimization flag, and the -O is the optimization level. Hence, some -O level may include the flag -fno-elide-constructors. Am I right?

In other words, is there any relationship between -fno-elide-constructors and -O..?

解决方案

In other words, is there relationships between -fno-elide-constructors and -O..?

Yes there is a relationship, although a quite simple one: gcc will most likely elide constructors already at the -O0 level unless you explicitly disable it. See the example code at the bottom for proof.

It is tricky though, the compiler can do really nasty things, see RVO force compilation error on failure. The bottom line is: You always need to check the generated assembly to see what is really happening under the hood.

Please keep in mind (from Working Draft, Standard for Programming Language C++, N3797 which is the closest approximation of C++14 that I could find):

12.8/31 When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. [...]


The code substantiating my statement:

#include <cstdio>
constexpr int size = 1024;

struct A { int array[size] = { 0 }; };

int main() {
  A a = A();
  std::printf("%d\n", a.array[size-1]);
}


With g++ -std=c++11 -Wall -O0 -S elide.cpp, in the generated assembly code there is only a single

    call    A::A()

However, with g++ -std=c++11 -Wall -O0 -fno-elide-constructors -S elide.cpp I get:

    call    A::A()
    [...]
    call    A::A(A&&)

Even if you disable optimizations with -O0, you still have to disable elision additionally if you need it to be disabled for some reason.

这篇关于-O0或其他优化级别中包含-fno-elide-constructors?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆