我看到某个地方有人决定复制一个对象并随后将其移动到一个类的数据成员的代码.这使我感到困惑,因为我认为移动的重点是避免复制.这是一个例子:
struct S
{
S(std::string str) : data(std::move(str))
{}
};
Run Code Online (Sandbox Code Playgroud)
这是我的问题:
str?std::string吗?最近我从cppreference中读到了一个例子.../vector/emplace_back:
struct President
{
std::string name;
std::string country;
int year;
President(std::string p_name, std::string p_country, int p_year)
: name(std::move(p_name)), country(std::move(p_country)), year(p_year)
{
std::cout << "I am being constructed.\n";
}
Run Code Online (Sandbox Code Playgroud)
我的问题:这std::move真的需要吗?我的观点是,这p_name不是在构造函数体中使用,所以,也许,语言中有一些规则默认使用移动语义?
将std :: move初始化列表添加到每个重要成员(例如std::string,std::vector)会非常烦人.想象一下用C++ 03编写的数百个KLOC项目 - 我们是否应该添加到处std::move?
这个问题:move-constructor-and-initialization-list答案说:
作为一个黄金法则,无论何时你通过右值引用来获取内容,你需要在std :: move中使用它,并且每当你通过通用引用(即用&&推导出模板化类型)时,你需要在std ::里面使用它向前
但我不确定:通过价值而不是普遍的参考?
[UPDATE]
使我的问题更清楚.构造函数参数可以被视为XValue - 我的意思是到期值?
在这个例子AFAIK我们不使用std::move:
std::string getName()
{
std::string local = "Hello SO!";
return local; // std::move(local) is not needed nor …Run Code Online (Sandbox Code Playgroud) c++ language-lawyer initialization-list move-semantics c++11
考虑:
#include <cstdlib>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
class Gizmo
{
public:
Gizmo() : foo_(shared_ptr<string>(new string("bar"))) {};
Gizmo(Gizmo&& rhs); // Implemented Below
private:
shared_ptr<string> foo_;
};
/*
// doesn't use std::move
Gizmo::Gizmo(Gizmo&& rhs)
: foo_(rhs.foo_)
{
}
*/
// Does use std::move
Gizmo::Gizmo(Gizmo&& rhs)
: foo_(std::move(rhs.foo_))
{
}
int main()
{
typedef vector<Gizmo> Gizmos;
Gizmos gizmos;
generate_n(back_inserter(gizmos), 10000, []() -> Gizmo
{
Gizmo ret;
return ret;
});
random_shuffle(gizmos.begin(), gizmos.end());
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,有两个版本Gizmo::Gizmo(Gizmo&&) …