Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler optimization or my misunderstanding

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;
        }
};
like image 781
Validus Oculus Avatar asked Dec 19 '22 20:12

Validus Oculus


1 Answers

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

like image 103
Shafik Yaghmour Avatar answered Dec 22 '22 10:12

Shafik Yaghmour