compiler optimization

前端 未结 4 478
耶瑟儿~
耶瑟儿~ 2021-02-05 23:01

So I have a question for you. :) Can you tell me the output the following code should produce?

#include 
struct Optimized
{
    Optimized() { std         


        
相关标签:
4条回答
  • 2021-02-05 23:32

    The output this code will produce is unpredictable, since the language specification explicitly allows optional elimination (elision) of "unnecessary" temporary copies of class objects even if their copy constructors have side effects.

    Whether this will happen or not might depend on may factors, including the compiler optimization settings.

    In my opinion calling the above copy elision an "optimization" is not entirely correct (although the desire to use this term here is perfectly understandable and it is widely used for this purpose). I'd say that the term optimization should be reserved to situations when the compiler deviates from the behavior of the abstract C++ machine while preserving the observable behavior of the program. In other words, true optimization implies violation of the abstract requirements of the language specification. Since in this case there's no violation (the copy elision is explicitly allowed by the standard), there's no real "optimization". What we observe here is just how the C++ language works at its abstract level. No need to involve the concept of "optimization" at all.

    0 讨论(0)
  • 2021-02-05 23:32

    Even when passing back by value the compiler can optimise the extra copy away using Return Value Optimisation see; http://en.wikipedia.org/wiki/Return_value_optimization

    0 讨论(0)
  • 2021-02-05 23:38

    Both outputs are permissible. The C++03 language standard says, in clause 12.8/15:

    When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy 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.111) This elision of copy operations is permitted in the following circumstances (which may be combined to eliminate multiple copies):

    • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function’s return value
    • when a temporary class object that has not been bound to a reference (12.2) would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy
    0 讨论(0)
  • 2021-02-05 23:44

    This is known as Copy Elision and is a special handling instead of copying/moving.

    The optimization is specifically allowed by the Standard, as long as it would be possible to copy/move (ie, the method is declared and accessible).

    The implementation in a compiler is generally referred to, in this case, as Return Value Optimization. There are two variations:

    • RVO: when you return a temporary (return "aa" + someString;)
    • NRVO: N for Named, when you return an object that has a name

    Both are implemented by major compilers, but the latter may kick in only at higher optimization levels as it is more difficult to detect.

    Therefore, to answer your question about returning structs: I would recommend it. Consider:

    // Bad
    Foo foo;
    bar(foo);
    
    -- foo can be modified here
    
    
    // Good
    Foo const foo = bar();
    

    The latter is not only clearer, it also allows const enforcement!

    0 讨论(0)
提交回复
热议问题