简体   繁体   English

为什么不能-O0禁用gcc编译优化?

[英]Why can't -O0 disable gcc compile optimization?

string str="fujian";

Some books say that code will trigger the copy constructor, but g++ will optimize it so that the copy constructor won't be called. 有些书说代码会触发复制构造函数,但是g ++会对它进行优化,以便不会调用复制构造函数。

However, I used g++ command -O0 to disable the optimization, but it still can't trigger the copy constructor. 但是,我使用g ++命令-O0来禁用优化,但它仍然无法触发复制构造函数。

How to understand it? 怎么理解呢?

使用GCC和Clang,您可以使用-fno-elide-constructors elide -fno-elide-constructors编译标志来关闭复制/移动省略优化。

The copy elision rule is based on ISO C++ 12.8. 复制省略规则基于ISO C ++ 12.8。 While other rules used generally for optimization are collectively called as "as-if" rule in clause 1 (which allows the implementation generates program behave somewhat differently with the "non-optimized" program semantics based on abstract machine model), this rule is so special that you can treat the "optimized" code itself behaves as exact as the original meaning. 虽然通常用于优化的其他规则在第1节中统称为“as-if”规则(这允许实现生成程序与基于抽象机器模型的“非优化”程序语义略有不同),但这个规则是如此特殊的,您可以对待“优化”代码本身的行为与原始含义一样精确。 In other words, the elided constructor calls may not exist at all in the abstract machine's behavior. 换句话说,抽象机构的行为中可能根本不存在省略的构造函数调用。

If there is no undefined behavior, with or without optimization according to the as-if rules, the observable behavior of the optimized program and non-optimized program should be same (although they may differ on performance, etc). 如果没有未定义的行为,根据as-if规则进行或不进行优化,优化程序和非优化程序的可观察行为应该相同(尽管它们可能在性能上有所不同等)。 However, copy elision is more aggressive, namely, it can alter the observable behavior. 但是,复制省略更具侵略性,即它可以改变可观察的行为。

You'd better not rely on the differences produced by copy elision. 你最好不要依赖复制省略产生的差异。 Thus, it is reasonable to keep same behavior for ordinary optimization options and provide a separate option to control the precise (different) behavior for users who know the risks well and indeed need it. 因此,对于普通优化选项保持相同的行为是合理的,并提供单独的选项来控制那些知道风险并确实需要它的用户的精确(不同)行为。

WG21/N4296 WG21 / N4296

1.9 Program execution 1.9程序执行

5 A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input. 5执行格式良好的程序的符合实现应该产生与具有相同程序和相同输入的抽象机的相应实例的可能执行之一相同的可观察行为。 However, if any such execution contains an undefined operation, this International Standard places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation). 但是,如果任何此类执行包含未定义的操作,则此国际标准不要求使用该输入执行该程序的实现(甚至不考虑第一个未定义操作之前的操作)。

8 The least requirements on a conforming implementation are: 8对符合要求的实施的最低要求是:

(8.1) — Access to volatile objects are evaluated strictly according to the rules of the abstract machine. (8.1) - 严格按照抽象机的规则评估对易失性对象的访问。

(8.2) — At program termination, all data written into files shall be identical to one of the possible results that execution of the program according to the abstract semantics would have produced. (8.2) - 在程序终止时,写入文件的所有数据应与根据抽象语义产生的程序执行的可能结果之一相同。

(8.3) — The input and output dynamics of interactive devices shall take place in such a fashion that prompting output is actually delivered before a program waits for input. (8.3) - 交互设备的输入和输出动态应以在程序等待输入之前提示输出实际传送的方式进行。 What constitutes an interactive device is implementation-defined. 构成交互设备的是实现定义的。

These collectively are referred to as the observable behavior of the program. 这些统称为程序的可观察行为 [ Note: More stringent correspondences between abstract and actual semantics may be defined by each implementation. [注意:每个实现可以定义抽象和实际语义之间更严格的对应关系。 —end note ] - 尾注]

12.8 Copying and moving class objects 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. 31当满足某些条件时,允许实现省略类对象的复制/移动构造,即使为复制/移动操作选择的构造函数和/或对象的析构函数具有副作用。 In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.122 This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies): 在这种情况下,实现将省略的复制/移动操作的源和目标视为仅仅两种不同的引用同一对象的方式,并且该对象的销毁发生在两个对象的后期时间。在没有优化的情况下销毁.122在下列情况下允许复制/移动操作(称为复制省略)的这种省略(可以合并以消除多个副本):

... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在cppFlags中添加“ -O0”无法在android studio中禁用clang编译优化 - adding “-O0” to cppFlags fails to disable clang compile optimization in android studio 为什么在 O0 上编译时,c++ std::min 不能使用静态字段作为其参数? - why does c++ std::min can't use a static field as its parameter when compile on O0? 为什么 GCC 删除了我在 O3 上的代码,而不是在 O0 上? - Why does GCC delete my code on O3 but not on O0? 为什么gcc -O1的行为与gcc -O0 +选项不同 - Why does gcc -O1 behave differently than gcc -O0 + options GCC中带有-O3的共享库编译不会导出与-O0中一样多的符号 - Shared library compilation in GCC with -O3 don't export as much symbols as in -O0 为什么我的程序在O0和O2的优化级别上返回不同的结果 - Why my program return different results at optimization level of O0 and O2 G ++不会使用-O0编译我的源代码,但是会使用-O2 -DNDEBUG编译我的源代码,我该如何解决呢? - G++ does not compile my source code with -O0 but it does with -O2 -DNDEBUG, how can I solve it? gcc -O0与-Og编译时间 - gcc -O0 vs. -Og compilation time gcc -O4优化标志 - gcc -O4 optimization flag g++ O1 不等于带有所有相关优化标志的 O0 - g++ O1 is not equal to O0 with all related optimization flags
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM