Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 安全ptr实现_C++_Pointers_C++11_Smart Pointers - Fatal编程技术网

C++ 安全ptr实现

C++ 安全ptr实现,c++,pointers,c++11,smart-pointers,C++,Pointers,C++11,Smart Pointers,我正在尝试实现std::shared_ptr的一个安全版本,称为“safe_ptr”,它保证“非空” 编辑:删除问题。如果感兴趣,请参阅编辑。向任何感兴趣的人发布最终解决方案: 此代码现在托管在上 #pragma一次 #包括 #包括 #包括 模板 安全等级 { 模板好友类安全\u ptr; 公众: 类型定义T元素_类型; safe_ptr():impl_(std::make_shared()){ safe_ptr(const safe_ptr&other):impl_(other.impl

我正在尝试实现std::shared_ptr的一个安全版本,称为“safe_ptr”,它保证“非空”

编辑:删除问题。如果感兴趣,请参阅编辑。向任何感兴趣的人发布最终解决方案:

此代码现在托管在上

#pragma一次
#包括
#包括
#包括
模板
安全等级
{   
模板好友类安全\u ptr;
公众:
类型定义T元素_类型;
safe_ptr():impl_(std::make_shared()){
safe_ptr(const safe_ptr&other):impl_(other.impl_){}
模板
safe_ptr(const safe_ptr&other,typename std::enable_if::type=0):impl_(other.impl_){
模板
安全\U ptr(常量U&impl,类型名称std::enable\U if::type*=0)
:impl_(std::make_shared(impl)){
模板
安全ptr(常量U&impl,数据类型,类型名称std::enable_如果::类型*=0)
:impl_U(新的U(impl),dtor){}
模板
安全ptr(U&&impl,typename std::enable_if::type*=0)
:impl_(std::make_shared(std::forward(impl))){
模板
安全\U ptr(U&&impl,D dtor,typename std::enable\U if::type*=0)
:impl_uu(newu(std::forward(impl)),dtor{}
模板
显式安全\u ptr(const std::shared\u ptr&impl,typename std::enable\u if::type=0):impl(impl)
{
如果(!impl_)
throw std::无效的_参数(“impl”);
}
模板
显式安全\u ptr(std::shared\u ptr&&impl,typename std::enable\u if::type=0):impl(std::move(impl))
{
如果(!impl_)
throw std::无效的_参数(“impl”);
}
模板
显式安全\U ptr(U*impl,typename std::enable\U if::type=0):impl(impl)
{
如果(!impl_)
throw std::无效的_参数(“impl”);
}
模板
显式安全ptr(U*impl,D dtor,typename std::enable_if::type=0):impl(impl,dtor)
{
如果(!impl_)
throw std::无效的_参数(“impl”);
}
模板
typename std::enable_if::type
操作员=(const safe_ptr&other)
{
安全温度(其他);
临时交换(*本);
归还*这个;
}
模板
typename std::enable_if::type
运算符=(U&&impl)
{
安全ptr温度(标准:正向(impl));
临时交换(*本);
归还*这个;
}
T&运算符*()常量{return*impl_.get();}
T*运算符->()常量{return impl_uu.get();}
T*get()常量{return impl_.get();}
bool unique()常量{return impl_uuu.unique();}
long use_count()常量{return impl.use_count();}
无效交换(safe_ptr&other){impl_.swap(other.impl_)}
运算符std::shared_ptr()常量{return impl}
模板
bool owner_before(const safe_ptr&ptr){return impl.owner_before(ptr.impl)}
模板
bool owner_before(const std::shared_ptr&ptr){return impl.owner_before(ptr);}
模板
D*get_deleter(safe_ptr const&ptr){return impl.get_deleter()}
私人:
std::共享\u ptr impl;
};
模板
布尔运算符==(常数安全\u ptr&a、常数安全\u ptr&b)
{
返回a.get()==b.get();
}
模板
接线员=(施工安全检查与维护、施工安全检查与维护)
{
返回a.get()!=b.get();
}
模板
bool操作员(施工安全检查与维护、施工安全检查与维护)
{
返回a.get()>b.get();
}
模板
布尔运算符>=(常数安全\u ptr&a、常数安全\u ptr&b)
{
返回a.get()>=b.get();
}
模板

布尔运算符我认为您使用的是
可转换的
反标记。试一试

template<typename Y>
safe_ptr(const safe_ptr<Y>& other, typename std::enable_if<std::is_convertible<Y*, T*>::value, void*>::type = 0) : impl_(other.impl_){}
模板
safe_ptr(const safe_ptr&other,typename std::enable_if::type=0):impl_(other.impl_){

您得到了
可向后转换的参数。您要检查
Y*
T*
,其表示为:

std::is_convertible<Y*, T*>
std::是否可转换
关于您的编辑:是的,您需要朋友声明

// Within the body of the class
template <typename> friend class safe_ptr; // the syntax is peculiar...
//在类的主体中
模板好友类安全\u ptr;//语法很奇特。。。

此外,您可能希望为
safe\u ptr
提供一个默认构造函数,它自然会默认构造对象。我忘记了调用我在回答您之前的问题时所指对象的默认构造函数:)

要访问私有成员
impl\u
,您必须熟悉
safe\u ptr
模板的不同实例化

template <class Y> friend class safe_ptr;
模板好友类安全\u ptr;
模板朋友级安全\u ptr;

漂亮的接球。。。但是现在我有一个新问题。。。请参见编辑。@ronag:是的,这是一个可访问性问题,朋友声明可以解决这个问题。马修在回答中举了一个例子。+1干得好。你是在任何许可证下发布的吗?我想它应该是在GNU通用公共许可证3或更高版本下发布的。如果您在某个项目中使用它,我也将非常感谢您的邮件。
template <class Y> friend class safe_ptr;
template<class Y> friend class safe_ptr;