如何正确地将右值绑定到构造函数?

How to properly bind rvalues to the constructor?

这是我的代码:

#include <iostream>


class dummy{
    public:
    //constructor + destructor
    dummy(){
    std::cout<<"dummy constructed"<<std::endl;
    }


    //copy 
    dummy(const dummy& o){
    std::cout<<"dummy copy init"<<std::endl;
    }


    void operator=(const&dummy){
    std::cout<<"dummy copy operator"<<std::endl;
    }

    //moves
    dummy(dummy&& other){
    std::cout<<"dummy move init"<<std::endl;        
    }

    void operator=(dummy&&){
    std::cout<<"dummy move copy"<<std::endl;
    }

};



class test{
    public:
    dummy d;

    test(dummy&& d):d(std::move(d)){
    std::cout<<"test"<<std::endl;   
    }
};




int main(int argc, char** argv) {

    test m(dummy());        //prints NOTHING.
    test t(std::move(dummy()));

    return 0;
}

使用test m(dummy());

输出:

使用test t(std::move(dummy()));

输出:

dummy constructed
dummy move init
test

这是意料之中的事情。

所以我的问题是,

如果参数是type&&,是否必须使用std::move? 并且 dummy() 不被视为右值,所以为什么我需要使用 std::move ?我对将右值绑定到右值引用有些困惑,我想需要一些说明。

test m(dummy()); 声明了一个名为 m 的函数。你的意思可能是:

test m{ dummy{} };

声明一个变量 m 传递一个临时变量 dummy