使用const_cast实现move构造函数
Use const_cast to implement the move constructor
我已经阅读了关于move构造函数的这一页。在那篇文章中,我们使用模板proxy
来修改从函数返回的临时变量。
这是实现。
namespace detail {
template <class T>
struct proxy
{
T *resource_;
};
} // detail
template <class T>
class MovableResource
{
private:
T * resource_;
public:
explicit MovableResource (T * r = 0) : resource_(r) { }
~MovableResource() throw() { delete resource_; } // Assuming std:::auto_ptr like behavior.
MovableResource (MovableResource &m) throw () // The "Move constructor" (note non-const parameter)
: resource_ (m.resource_)
{
m.resource_ = 0; // Note that resource in the parameter is moved into *this.
}
MovableResource (detail::proxy<T> p) throw () // The proxy move constructor
: resource_(p.resource_)
{
// Just copying resource pointer is sufficient. No need to NULL it like in the move constructor.
}
MovableResource & operator = (MovableResource &m) throw () // Move-assignment operator (note non-const parameter)
{
// copy and swap idiom. Must release the original resource in the destructor.
MovableResource temp (m); // Resources will be moved here.
temp.swap (*this);
return *this;
}
MovableResource & operator = (detail::proxy<T> p) throw ()
{
// copy and swap idiom. Must release the original resource in the destructor.
MovableResource temp (p);
temp.swap(*this);
return *this;
}
void swap (MovableResource &m) throw ()
{
std::swap (this->resource_, m.resource_);
}
operator detail::proxy<T> () throw () // A helper conversion function. Note that it is non-const
{
detail::proxy<T> p;
p.resource_ = this->resource_;
this->resource_ = 0; // Resource moved to the temporary proxy object.
return p;
}
};
如果只是添加一个构造函数,接受const引用,并使用const_cast
来改变变量,以实现像这样的移动语义。
MovableResource(const MovableResource& m)
{
MovableResource& afterM = const_cast<MovableResource&>(m);
afterM.swap(*this);
}
这会引入未定义行为吗?
在复制构造函数接口下实现move语义只是自找麻烦。通过使用const_cast
,您可以有效地欺骗接口的客户端,使其相信您的构造函数在最终修改其值时不会修改m
。考虑下面的代码:
const MovableResource first(/* Some resource 'A' */);
MovableResource second(first);
// 'first', supposedly 'const' now has potentially a different value!
MovableResource (MovableResource &m) throw()//"Move构造函数"(注意非const参数)
不是move构造函数,而是copy构造函数。正确的move构造函数接受右值引用作为输入:
MovableResource (MovableResource &&m) throw ()
同样,MovableResource,operator = (MovableResource &m) throw()//移动赋值操作符(注意非const形参)
也是一个复制赋值操作符。正确的move赋值操作符接受右值引用作为输入:
MovableResource & operator = (MovableResource &&m) throw ()
相关文章:
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 添加自定义析构函数时,Move 构造函数在派生类中消失
- std::映射,只有move构造函数可用
- 为什么我的代码中没有调用move构造函数
- std::转换move构造函数的模板专业化的变体
- 如何调用move构造函数
- 我可以将 std::move 与不提供 move 构造函数的类一起使用吗?
- 为什么noexcept move构造函数在向量重新分配期间没有被调用
- 为什么在声明析构函数时必须声明 copy & move 构造函数?
- 如何在我自己的类 Vector 中使用 Move 构造函数而不是 Move 赋值运算符
- GoogleTest Move 构造函数覆盖率
- C - 构造函数,复制构造函数,MOVE构造函数,驱动器
- 如何在基类中调用 "move" '&&' 构造函数?在C++
- 如果我有一个向量(或类似的东西)成员变量,那么move构造函数看起来怎么样
- std::vector的move构造函数是否调用项的move构造器
- 为什么我们需要在move构造函数中将右值引用设置为null
- move构造函数在C++中调用了两次吗
- 实现move构造函数如何影响返回值优化
- 不能默认使用Move构造函数
- C++合成的move构造函数如何受到volatile和虚拟成员的影响