防止移动unique_ptr C ++ 11 [英] Prevent moving of a unique_ptr C++11

查看:140
本文介绍了防止移动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_ptrboost::scoped_ptr

这篇关于防止移动unique_ptr C ++ 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆