为什么移动构造函数不调用

why move Constructor is not call?

本文关键字:调用 构造函数 移动 为什么      更新时间:2023-10-16

请看下面的示例代码:

    class testo
{
public:
    testo()
    {
        cout << " default " << endl;
    }
    testo(const testo & src)
    {
        cout << "copy " << endl;
    }
    testo(const testo && src)
    {
        cout << "move" << endl;
    }
    testo & operator=(const testo & rhs)
    {
        cout << " assigment" << endl;
        return *this;
    }
    testo & operator= (const testo && rhs)
    {
        cout << "move" << endl;
    }
};

这是我的函数和主代码:

testo nothing(testo & input)
{
return input;
}
int main ()
{
testo boj1 ;
testo obj2(nothing(obj1) );
return 1;
}

当我编译并运行此代码时,我希望看到:

default    // default constructor
copy       // returning from the function
move       // moving to the obj2

但是当代码被执行时,它只是显示:

default 
copy

编译器是Visual C++ 2015

移动签名应该定义为T&&,而不是T const &&。虽然语言中没有任何内容阻止你声明T const &&的东西,但实际上没有任何意义:你的意思是离开这个对象,但它const,因此不能改变它的状态?这是一个术语的矛盾。