简体   繁体   中英

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.

However, I used g++ command -O0 to disable the optimization, but it still can't trigger the copy constructor.

How to understand it?

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

The copy elision rule is based on 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. 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). 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

1.9 Program execution

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. 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.1) — Access to volatile objects are evaluated strictly according to the rules of the abstract machine.

(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.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. 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

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. 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):

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM