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 6993209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:44:08+00:00 2026-05-27T19:44:08+00:00

Lets say I want to implement a smart pointer a_ptr which can be compared

  • 0

Lets say I want to implement a smart pointer a_ptr which can be compared with other smart pointers.

Then I need to implement all permutations of the comparison operators:

template<class T, class U>
bool operator==(const a_ptr<T>& a, const a_ptr<U>& b)
{
    return a.get() == b.get();
}

template<class T, class U>
bool operator==(const std::shared_ptr<T>& a, const a_ptr<U>& b)
{
    return a.get() == b.get();
}

template<class T, class U>
bool operator==(const a_ptr<T>& a, const std::shared_ptr<U>& b)
{
    return a.get() == b.get();
} 

and etc… for the rest of the operators.

Then maybe I would like to implement another smart pointer b_ptr, which would give me 6 versions for every comparison operator (since I want it to also work with a_ptr), clearly not manageable.

Is there any way to get around this problem?

EDIT:

I should have probably mentioned that I want to create wrappers around smart pointers, in which case this question makes more sense, e.g.

template<typename T>
class a_ptr
{
public:
    const T* get() const {return p_.get();}
private:
    std::shared_ptr<T> p_;
};

template<typename T>
class b_ptr
{
public:
    const T* get() const {return p_.get();}
private:
    a_ptr<T> p_;
};
  • 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-05-27T19:44:09+00:00Added an answer on May 27, 2026 at 7:44 pm

    How about:

    template<typename T> struct you_need_a_pointer_to_do_that{} unwrap_ptr(const T&);
    
    template<typename U> U* unwrap_ptr(U* p) { return p; }
    
    template<typename U> U* unwrap_ptr(const a_ptr<U>& p) { return p.get(); }
    
    template<typename U> U* unwrap_ptr(const unique_ptr<U>& p) { return p.get(); }
    

    …

    template<typename T, typename PU> auto operator<(const a_ptr<T>& a, const PU& b) -> decltype(unwrap_ptr(a) < unwrap_ptr(b)) { return unwrap_ptr(a) < unwrap_ptr(b); }
    

    The decltype bit rejects non-pointers due to SFINAE. And the unwrap_ptr helper homogenizes the syntax for pointer access, with complexity only linear in the number of pointer types.

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

Sidebar

Related Questions

Lets say you want to create a listing of widgets The Widget Manufacturers all
Lets say I have a single object of type Car which I want to
Lets say that I have a function todo() and I want to implement this
Lets say I want to define 2 styles. .color_red { color: Red; } .banner
Lets say we want a library of javascript-based pieces of functionality (I'm thinking jquery):
Lets say I want to make few classes to determine behaviour of agents. The
I would appreciate any help on this issue. Lets say I want to load
My simplified and contrived example is the following:- Lets say that I want to
Lets say I've got two interchangeable pieces of code and I want to figure
Lets say I have multiple DataGrids throughout my winform app and I want to

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.