可以继承C ++ 11智能指针并覆盖相对运算符吗? [英] Is it OK to inherit from the C++11 smart pointers and override the relative operators?

查看:172
本文介绍了可以继承C ++ 11智能指针并覆盖相对运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 cppreference.com std :: shared_ptr 提供了一整套相对运算符(==,!=,<,...),但未指定比较的语义。我假设他们将底层原始指针与引用的对象进行比较,并且std :: weak_ptr和std :: unique_ptr也是如此。

According to cppreference.com, std::shared_ptr provides a full set of relative operators (==, !=, <, ...), but the semantics of comparison aren't specified. I assume they compare the underlying raw pointers to the referenced objects, and that std::weak_ptr and std::unique_ptr do the same.

出于某些目的,我更愿意具有相对运算符,基于比较引用的对象(而不是指向它们的指针)来对智能指针进行排序。这已经是我做了很多事情,但是我自己的哑指针除了相对运算符外,其行为大部分都像原始指针。我也想用标准的C ++ 11智能指针做同样的事情。所以...

For some purposes, I would prefer to have relative operators that order the smart pointers based on comparing the referenced objects (rather than the pointers to them). This is already something I do a lot, but with my own "dumb pointers" that behave mostly like raw pointers except for the relative operators. I'd like to do the same thing with the standard C++11 smart pointers too. So...


  1. 继承C ++ 11智能指针(shared_ptr,weak_ptr和unique_ptr)是否可以覆盖相对运营商?

  1. Is it OK to inherit from the C++11 smart pointers (shared_ptr, weak_ptr and unique_ptr) and override the relative operators?

我需要注意哪些鬼鬼祟祟的问题?例如,我是否需要实现任何其他方法或使用使用来确保工作正常?

Are there any sneaky issues I need to look out for? For example, are there any other methods I need to implement or use using for to ensure things work correctly?

对于最终的懒惰,是否有可以自动为我执行此操作的库模板?

For the ultimate in laziness, is there a library template available that will do this for me automatically?

我希望这是当然你可以做到这一点,白痴!有点事,但我有点不确定,因为标准库中有一些类(至少像 std :: map 这样的容器),你不应该这样做继承自。

I'm hoping this is an "of course you can do that, idiot!" kind of thing, but I'm a little uncertain because there are some classes in the standard library (containers like std::map at least) that you're not supposed to inherit from.

推荐答案

一般来说,继承任何析构函数不动态的东西是不安全的。它可以并且通常完成,你必须非常小心。
我只是使用组合,而不是从指针继承,特别是因为成员数量相对较少。
您可以为此

In general, it's not safe to inherit from anything who's destructor is not dynamic. It can be and is done commonly, you just have to be really careful. Instead of inheriting from the pointers, I'd just use composition, especially since the number of members is relatively small. You might be able to make a template class for this

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;
};

显然,包装器的简单性会降低智能指针的最低分母,但无论如何。它们并不完全复杂,您可以为每个智能指针类创建一个。

Obviously, the simplicity of the wrapper reduces you to the lowest common denominator for the smart pointers, but whatever. They're not exactly complicated, you could make one for each of the smart pointer classes.

我将提供一个警告,我不喜欢 == 有效,因为对于指向不同对象的两个指针,它可能返回true。但是无所谓。我还没有测试过代码,它可能会因某些任务而失败,例如当它包含unique_ptr时尝试复制。

I will provide a warning that I don't like the way == works, since it may return true for two pointers to different objects. But whatever. I also haven't tested the code, it might fail for certain tasks, like attempting to copy when it contains a unique_ptr.

这篇关于可以继承C ++ 11智能指针并覆盖相对运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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