Compiler optimization or my misunderstanding

I recently tested the deep and dark corners of C ++, and I got confused in one subtle question. My test is so simple:

// 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 is from the line above; the first call to the constructor howmany()will create one temporary object, and then the compiler will use this temporary object with the copy constructor to create an instance of t . However, the compiler output really confused me, because the output shows only one constructor call. My friends mentioned me about optimizing the compiler profile, but we are not sure about that. I want to know what is going on here?

The result of task 2 is given below. Problem 1 goes completely beyond the scope of the object, because the compiler behaves like a function pointer declaration.

howmany()
~howmany()

:

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;
        }
};
+4
3

:

howmany t( howmany() );

, :

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

clang :

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() );
          ^
          (        )

- ++ 11 :

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

2, , , /, 12.8 31, :

/ , , / / , . / , , .122 /, ,

:

, (12.2), / cv- , / /

+9

, . , !

+1

, . , 2, , howmany() call -, copy-construction, .

-fno-elide-constructors - g++, . cmake .

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

, .

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

:)

+1

All Articles