【发布时间】:2016-09-08 12:53:53
【问题描述】:
考虑以下代码:
#include <iostream>
#include <string>
struct my_struct {
void func(std::string&& str) {
str_ = str;
}
std::string str_;
};
int main() {
my_struct s;
std::string str("Hello");
s.func(std::move(str));
std::cout << str << std::endl;
std::cout << s.str_ << std::endl;
}
为什么我在my_struct::func 中需要一个额外的std::move 才能调用std::string 的移动赋值运算符?额外的std::move 究竟会做什么?我认为它只会将给定类型转换为其右值引用对应物?
【问题讨论】:
标签: c++ c++11 move-semantics