且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

移动构造函数似乎没有执行

更新时间:2022-12-13 16:28:20

这不是很明显吗?您的代码会返回本地临时 c>窗口 c:

副本

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

编译器事实上会优化这个拷贝(通过返回值优化)这就是为什么你的移动构造函数从未实际调用 - 但为了正确性,一个副本构造函数仍然必须存在。


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 returns a Window object by value. The caller uses it to initialize a Window object. If I understood correctly, this should invoke the move constructor. In order to detect this I throw an exception there. On top of that I disabled the copy constructor:

#include <iostream>


// Fake WinAPI
typedef void* HWND;
HWND CreateWindow() { return (void*)1; }
void DestroyWindow(HWND) { }
// End WinAPI


// C++ WinAPI Wrapper Library
class Window
{
public:
    Window(HWND inHandle) :
        mHandle(inHandle)
    {
        std::cout << "Window constructor. Handle: " << inHandle << std::endl;
    }

    Window(Window && rhs) :
        mHandle(rhs.mHandle)
    {
        std::cout << "Window move constructor. Handle: " << mHandle << std::endl;
        rhs.mHandle = 0;
        throw 1; // this is my "breakpoint"
    }

    ~Window()
    {
        std::cout << "Window destructor. Handle: " << mHandle << std::endl;
        if (mHandle)
        {
            DestroyWindow(mHandle);
        }
    }

private:
    Window(const Window&);
    Window& operator=(const Window&);

    HWND mHandle;
};


// Factory function
Window MakeWindow()
{
    return Window(CreateWindow());
}


int main()
{

    {
        Window window(MakeWindow());
    }
    std::cout << "Everything is OK." << std::endl;
    return 0;
}

However the code runs fine without this exception being thrown. This is the console output:

Window constructor. Handle: 0x1
Window destructor. Handle: 0x1
Everything is OK.

If I comment out the move constructor then compilation fails with the following errors:

MysteryMove.cpp: In function 'Window MakeWindow()':
MysteryMove.cpp:39:5: error: 'Window::Window(const Window&)' is private
MysteryMove.cpp:49:33: error: within this context
MysteryMove.cpp: In function 'int main()':
MysteryMove.cpp:39:5: error: 'Window::Window(const Window&)' is private
MysteryMove.cpp:57:35: error: within this context
make: *** [all] Error 1

It doesn't seem to make sense. Can anyone explain what is going on?

Update

Thanks to @Philipp I learned that move constructors can also be omitted. This is described in §12.8/34 and footnote 124 of the N3126 draft standard.

It is there also mentioned that RVO is only allowed for non-volatile objects. This means I can get around it writing the factory function like this:

// Factory function
Window MakeWindow()
{
    volatile Window window(CreateWindow());
    return const_cast<Window&&>(window);
}

And indeed it works:

Window constructor. Handle: 0x1
Window move constructor. Handle: 0x1
terminate called after throwing an instance of 'int'
Abort trap

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.