Compiler optimization or my misunderstanding

2019-06-26 05:27发布

问题:

Recently I was testing some C++ deep and dark corners and I got confused about one subtle point. My test is so simple actually:

// problem 1
// no any constructor call, g++ acts as a function declaration to the (howmany())
// g++ turns (howmany()) into (howmany(*)()) 
howmany t(howmany());

// problem 2
// only one constructor call
howmany t = howmany();

My expectation from above line was; first howmany() constructor call will produce one temporary object and then compiler will use that temporary object with copy-constructor in order to instantiate t. However, output of compiler really confused me because output shows only one constructor call. My friends mentioned me about compiler pass-by-value optimization but we are not sure about it. I want to learn what does happen here ?

Output of problem 2 is below. problem 1 is completely beyond object instantiation because compiler behaves it as a function pointer declaration.

howmany()
~howmany()

My test class is:

class howmany {
    public:
        howmany() {
            out << "howmany()" << endl;
        }
        howmany(int i) {
            out << "howmany(i)" << endl;
        }
        howmany(const howmany& refhm) {
            out << "howmany(howmany&)" << endl;
        }
        howmany& operator=(const howmany& refhm) {
            out << "operator=" << endl;
        }
        ~howmany() {
            out << "~howmany()" << endl;
        }
        void print1() {
            cout << "print1()" << endl;
        }
        void print2() {
            cout << "print2()" << endl;
        }
};

回答1:

This is the most vexing parse here:

howmany t( howmany() );

In order to fix this you need to add an extra set of parens:

howmany t( (howmany()) );
           ^         ^

clang is very helpful here and warns you:

warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
howmany t( howmany() );
        ^~~~~~~~~~~~~
main.cpp:31:12: note: add a pair of parentheses to declare a variable
howmany t( howmany() );
          ^
          (        )

The other way to fix this is to use C++11 uniform initialization syntax:

howmany t{ howmany{} };
         ^        ^^ ^ 

Update

To address part 2 which you added to the question the draft standard allows for omission of the copy/move construction in some cases. We can see this from section 12.8 Copying and moving class objects paragraph 31 which says:

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

and includes the following bullet:

when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move



回答2:

Yes, this is one of the optimization compiler may do. The compiler is allowed to eliminate temporary object creation even if copy constructor has side effects!



回答3:

After getting clues from above answers I found real problem. In my case, especially problem 2, compiler eliminates the copy construction process because return value of howmany() constructor call is completely same to the howmany t object, for that reason compiler just eliminates copy-construction process in order to make optimization. More detail of this problem is covered in wikipedia, please look to there.Return value optimization

-fno-elide-constructors is one of g++ flags which is responsible from disabling the optimizations. In cmake we just need to set it.

set(CMAKE_CXX_FLAGS "-fno-elide-constructors")

After disabling optimizations, the output of program turned what i was expecting.

howmany()
howmany(howmany&)
~howmany()
~howmany()

Thanks :)