Ste*_*314 17 c++ inheritance smart-pointers operator-overloading c++11
根据cppreference.com,std::shared_ptr提供了一整套相对运算符(==,!=,<,...),但未指定比较的语义.我假设他们比较了引用对象的底层原始指针,并且std :: weak_ptr和std :: unique_ptr也是如此.
出于某些目的,我宁愿让相对运算符根据比较引用的对象(而不是指向它们的指针)来对智能指针进行排序.这已经是我做了很多事情,但是我自己的"哑指针"除了相对运算符之外,其行为大部分都像原始指针.我也想用标准的C++ 11智能指针做同样的事情.所以...
是否可以从C++ 11智能指针(shared_ptr,weak_ptr和unique_ptr)继承并覆盖相对运算符?
我需要注意哪些鬼鬼祟祟的问题?例如,我需要实现或使用任何其他方法using来确保工作正常吗?
对于最终的懒惰,是否有可用的库模板自动执行此操作?
我希望这是"当然你可以做到这一点,白痴!" 有点事,但我有点不确定,因为标准库中有一些类(std::map至少是容器)你不应该继承.
Dav*_*eas 11
第一件事,正如其他人已经指出的那样,继承不是可行的方法.但是,不是接受的答案建议的复杂的包装器,我会做一些更简单的事情:为你自己的类型实现你自己的比较器:
namespace myns {
struct mytype {
int value;
};
bool operator<( mytype const& lhs, mytype const& rhs ) {
return lhs.value < rhs.value;
}
bool operator<( std::shared_ptr<mytype> const & lhs, std::shared_ptr<mytype> const & rhs )
{
// Handle the possibility that the pointers might be NULL!!!
// ... then ...
return *lhs < *rhs;
}
}
Run Code Online (Sandbox Code Playgroud)
在魔术,这是不是真的魔法都为参数依赖查找(亦称Koening查找或ADL).当编译器遇到函数调用时,它会将参数的名称空间添加到lookup中.如果对象是模板的实例化,则编译器还将添加用于实例化模板的类型的名称空间.所以在:
int main() {
std::shared_ptr<myns::mytype> a, b;
if ( a < b ) { // [1]
std::cout << "less\n";
} else {
std::cout << "more\n";
}
}
Run Code Online (Sandbox Code Playgroud)
在[1],并且由于a和b是对象用户定义的类型 (*) ADL将踢,它将同时添加std和 myns到查找集.然后它会找到的标准定义operator<为std::shared_ptr是:
template<class T, class U>
bool std::operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
Run Code Online (Sandbox Code Playgroud)
它还将添加myns和添加:
bool myns::operator<( mytype const& lhs, mytype const& rhs );
Run Code Online (Sandbox Code Playgroud)
然后,在查找完成后,重载决策启动,并且它将确定这myns::operator<是一个比std::operator<调用更好的匹配,因为它是一个完美匹配,在这种情况下非模板优先.然后它将调用您自己的operator<而不是标准的.
如果你的类型实际上是一个模板,这会变得有点复杂,如果它是,删除一个注释,我会扩展答案.
(*)这是一个小小的简化.因为operator<可以作为成员函数或自由函数实现,编译器将在内部检查std::shared_ptr<>成员operator<(不存在于标准中)和朋友.它也将看里面mytype的friend功能...等.但最终它会找到合适的.
一般来说,继承任何析构函数不动态的东西是不安全的.它可以并且通常完成,你只需要非常小心.我只使用合成,而不是继承指针,特别是因为成员数量相对较少.您可以为此创建模板类
template<class pointer_type>
class relative_ptr {
public:
typedef typename std::pointer_traits<pointer_type>::pointer pointer;
typedef typename std::pointer_traits<pointer_type>::element_type element_type;
relative_ptr():ptr() {}
template<class U>
relative_ptr(U&& u):ptr(std::forward<U>(u)) {}
relative_ptr(relative_ptr<pointer>&& rhs):ptr(std::move(rhs.ptr)) {}
relative_ptr(const relative_ptr<pointer>& rhs):ptr(std::move(rhs.ptr)) {}
void swap (relative_ptr<pointer>& rhs) {ptr.swap(rhs.ptr);}
pointer release() {return ptr.release();}
void reset(pointer p = pointer()) {ptr.reset(p);}
pointer get() const {return ptr.get();}
element_type& operator*() const {return *ptr;}
const pointer_type& operator->() const {return ptr;}
friend bool operator< (const relative_ptr& khs, const relative_ptr& rhs) const
{return std::less<element>(*lhs,*rhs);}
friend bool operator<=(const relative_ptr& khs, const relative_ptr& rhs) const
{return std::less_equal<element>(*lhs,*rhs);}
friend bool operator> (const relative_ptr& khs, const relative_ptr& rhs) const
{return std::greater<element>(*lhs,*rhs);}
friend bool operator>=(const relative_ptr& khs, const relative_ptr& rhs) const
{return std::greater_equal<element>(*lhs,*rhs);}
friend bool operator==(const relative_ptr& khs, const relative_ptr& rhs) const
{return *lhs==*rhs;}
friend bool operator!=(const relative_ptr& khs, const relative_ptr& rhs) const
{return *lhs!=*rhs;}
protected:
pointer_type ptr;
};
Run Code Online (Sandbox Code Playgroud)
显然,包装器的简单性会降低智能指针的最小公分母,但无论如何.它们并不完全复杂,你可以为每个智能指针类创建一个.
我将提供一个警告,我不喜欢这种方式==,因为它可能会返回两个指向不同对象的指针.但是无所谓.我还没有测试过代码,它可能会因某些任务而失败,例如在包含unique_ptr时尝试复制.
| 归档时间: |
|
| 查看次数: |
3737 次 |
| 最近记录: |