如下代码:从有两次构造和析构看,为什么emplace_back(std::move(stu))并没有移动呢?
class Stu{
public:
Stu() {
cout << "constuctor" << endl;
}
Stu(const Stu& stu){
cout << "copy constuctor" << endl;
}
Stu(const Stu&& stu){
cout << "move copy constuctor" << endl;
}
~Stu() {
cout << "destructor" << endl;
}
};
int main(){
vector<Stu> vec;
Stu stu;
vec.emplace_back(std::move(stu));
return 0;
}
移动构造应该是 Stu(Stu&& stu) ,没有 const。
移动构造是会修改参数的,不能是 const 。
而且,你这个移动了啊 : https://godbolt.org/z/da1cK4cvz