Visual Studio optimization even when optimization is disabled?

I talk with the move and copy constructors to find out a little more about them, and I come across this little weirdness that makes me scratch my head a bit. Basically, I created a class with a constructor, destructor, copy constructor, and constructor override, and I build them differently to see how the constructors are called. I found an order for calls, although this does not give the expected results. Keep in mind that optimization is completely disabled here, and I am compiling in VS2012. In this case, links are still possible?

Here is the source code I wrote.

class RvalueTest
{
public:
    RvalueTest() {
        printf("CONSTRUCTOR\n");
    }

    RvalueTest(const RvalueTest& r) {
        printf("COPY CONSTRUCTOR\n");
    }

    RvalueTest(RvalueTest&& r) {
        printf("MOVE CONSTRUCTOR\n");
    }
};

__declspec(noinline)
RvalueTest GetRvalueTest() {
    return RvalueTest();
}

Then I test it using the following code.

RvalueTest t1;
RvalueTest t2(t1);
RvalueTest t3(GetRvalueTest());

I expect to see the following.

CONSTRUCTOR
COPY CONSTRUCTOR
CONSTRUCTOR
MOVE CONSTRUCTOR

I really see it.

CONSTRUCTOR
COPY CONSTRUCTOR
CONSTRUCTOR

, , , . , ? , .

RvalueTest t1;
RvalueTest t2(t1);
RvalueTest t3(RvalueTest(GetRvalueTest()));

CONSTRUCTOR
COPY CONSTRUCTOR
+4
1

, move. , , , /, .

.

RvalueTest t3(RvalueTest(GetRvalueTest()));

t3 , RvalueTest(*)() RvalueTest . , (, VS2012)

RvalueTest t3(RvalueTest{GetRvalueTest()});
//                      ^               ^

, VS2013:

CONSTRUCTOR
COPY CONSTRUCTOR
CONSTRUCTOR
MOVE CONSTRUCTOR
+4

Source: https://habr.com/ru/post/1535662/


All Articles