由于我们在C++中移动了语义,所以现在通常这样做
void set_a(A a) { _a = std::move(a); }
Run Code Online (Sandbox Code Playgroud)
原因是,如果a是左值,则副本将被删除,并且只有一个移动.
但如果a是左值,会发生什么?似乎将有一个复制结构,然后是一个移动赋值(假设A有一个适当的移动赋值运算符).如果对象具有太多成员变量,则移动分配可能成本很高.
另一方面,如果我们这样做
void set_a(const A& a) { _a = a; }
Run Code Online (Sandbox Code Playgroud)
只有一个副本分配.如果我们传递左值,我们可以说这种方式优于传值的习语吗?
由于Visual Studio 2013中的这个错误,我需要提供自己的移动构造函数并移动派生类的赋值.但是,我不知道如何为基类调用适当的移动函数.
这是代码:
#include <utility>
// Base class; movable, non-copyable
class shader
{
public:
virtual ~shader()
{
if (id_ != INVALID_SHADER_ID)
{
// Clean up
}
}
// Move assignment
shader& operator=(shader&& other)
{
// Brett Hale's comment below pointed out a resource leak here.
// Original:
// id_ = other.id_;
// other.id_ = INVALID_SHADER_ID;
// Fixed:
std::swap( id_, other.id_ );
return *this;
}
// Move constructor
shader(shader&& other)
{
*this = std::move(other);
}
protected:
// Construct an …Run Code Online (Sandbox Code Playgroud) 我试图理解move-constructor的实现.我们都知道如果我们需要管理C++类中的资源,我们需要实现五阶规则(C++编程).
微软给我们举了一个例子:https://msdn.microsoft.com/en-us/library/dd293665.aspx
这是更好的一个,它使用copy-swap来避免代码重复: 动态分配一个对象数组
// C++11
A(A&& src) noexcept
: mSize(0)
, mArray(NULL)
{
// Can we write src.swap(*this);
// or (*this).swap(src);
(*this) = std::move(src); // Implements in terms of assignment
}
Run Code Online (Sandbox Code Playgroud)
在move-constructor中,直接:
// Can we write src.swap(*this);
// or (*this).swap(src);
Run Code Online (Sandbox Code Playgroud)
因为我觉得(*this) = std::move(src)有点复杂.因为如果我们(*this) = src无意中写,它会调用普通赋值运算符而不是move-assignment-operator.
除了这个问题,在微软的例子中,他们编写了这样的代码:在move-assignment-operator中,我们是否需要检查自我赋值?有可能发生吗?
// Move assignment operator.
MemoryBlock& operator=(MemoryBlock&& other)
{
std::cout << "In operator=(MemoryBlock&&). length = "
<< other._length << "." << std::endl;
if (this != &other)
{ …Run Code Online (Sandbox Code Playgroud)