Move constructor seemingly not executed

前端 未结 1 1432
借酒劲吻你
借酒劲吻你 2020-12-20 13:15

This is my first experiment with C++0x rvalue references and something strange seems to be going on.

In the code sample below the factory function MakeWindow

1条回答
  •  半阙折子戏
    2020-12-20 13:32

    Isn’t it obvious? Your code returns a copy of the local temporary Window:

    Window MakeWindow()
    {
        return Window(CreateWindow());
    }
    

    The compiler will in fact optimize this copy away (via return value optimization) – this is why your move constructor is never actually called – but for correctness a copy constructor must still be present.

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