Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:20:56+00:00 2026-06-11T18:20:56+00:00

According to cppreference.com , std::shared_ptr provides a full set of relative operators (==, !=,

  • 0

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.

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. Is it OK to inherit from the C++11 smart pointers (shared_ptr, weak_ptr and unique_ptr) and override the relative operators?

  2. 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?

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:20:57+00:00Added an answer on June 11, 2026 at 6:20 pm

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to cppreference.com , std::ios_base::failure has two inheritance hierarchies: Question Why and what is
According to http://en.cppreference.com/w/cpp/string/byte/memcpy C++'s memcpy takes three parameters: destination, source and size/bytes. It also
According to cppreference.com, the C++ static_cast operator's level of precedence is 2 . Why
According to docs at http://code.google.com/p/minify/wiki/UriRewriting $min_serveOptions['rewriteCssUris'] = false; I should be able to add
As according to cppreference : In inequality comparisons (<, >), the first elements are
According to Microsoft , you must sign your ClickOnce application. But it seems to
According to the MSDN article found at http://msdn.microsoft.com/en-us/library/wyk4d9cy.aspx the floating-point value .1 has no
According to the site, http://www.dba-oracle.com/t_nls_lang.htm Problem might occur even if both the database and
According to http://www.bennadel.com/blog/1892-You-Cannot-Bind-The-Submit-Event-To-Objects-Using-jQuery.htm It is a bug: I am pretty sure this is a
according to this article: http://net.tutsplus.com/tutorials/other/top-20-mysql-best-practices/ using ORDER BY RAND() is a bad idea because:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.