本文介绍了防止移动unique_ptr C ++ 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以防止用户使用
Is there any way to prevent a user to explicity take ownership of a unique pointer with
std::move
?
推荐答案
设为const
unique_ptr
move构造函数采用非常量右值引用,因此不能用const
对象调用.
The unique_ptr
move constructor takes a non-const rvalue reference, so can't be called with a const
object.
const unique_ptr<int> owner(new int);
// ...
unique_ptr<int> thief = std::move(owner); // ERROR
这允许unique_ptr
像boost::scoped_ptr
这篇关于防止移动unique_ptr C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!