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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T02:11:02+00:00 2026-05-13T02:11:02+00:00

I have a subset of a pointer class that look like: template <typename T>

  • 0

I have a subset of a pointer class that look like:

template <typename T>
struct Pointer
{
     Pointer();
     Pointer(T *const x);
     Pointer(const Pointer &x);
     template <typename t>
     Pointer(const Pointer<t> &x);

     operator T *() const;
};

The goal of the last constructor is to allow to pass a Pointer of a subclass, or basically any type that is implicitly convertable to T *. This actual rule is only enforced by the definition of the constructor and the compiler can’t actually figure it out by the declaration alone. If I drop it, and try to pass a Pointer<Sub> to a constructor of Pointer<Base>, I get a compile error, despite of the possible path through operator T *().

While it solves the above problem, it creates another one. If I have an overloaded function whose one overload takes a Pointer<UnrelatedClass> and the other takes Pointer<BaseClass>, and I try to invoke it with a Pointer<SubClass>, I get an ambiguity between the two overloads, with the intention, ofcourse, that the latter overload will be called.

Any suggestions? (Hopefully I was clear enough)

  • 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-13T02:11:02+00:00Added an answer on May 13, 2026 at 2:11 am

    The cure for your problem is called SFINAE (substitution failure is not an error)

    #include "boost/type_traits/is_convertible.hpp"
    #include "boost/utility/enable_if.hpp"
    
    template<typename T>
    class Pointer {
       ...
       template<typename U>
       Pointer(const Pointer<U> &x,
          typename boost::enable_if<
             boost::is_convertible<U*,T*>
          >::type* =0)
       : ...
       {
         ...
       }
       ...
    };
    

    If U* is convertible to T* the enable_if will have a typedef member type defaulting to void. Then, everything is fine. If U* is not convertible to T* this typedef member is missing, substitution fails and the constructor template is ignored.

    This solves your conversion and ambiguity problems.

    In response to the comment: is_convertible looks something like this:

    typedef char one;         // sizeof == 1  per definition
    struct two {char c[2];};  // sizeof != 1
    
    template<typename T, typename U>
    class is_convertible {
        static T source();
        static one sink(U);
        static two sink(...);
    public:
        static const bool value = sizeof(sink(source()))==1;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to create a subset of Users that don't have a login... basically
I have a std::vector that holds a Point struct (x,y,z and some other non-pointer
I have an entity that I'd like to compare with a subset and determine
I have read somewhere that Silverlight is subset of WPF , then why triggers
I'm running tomcat and have some jsp pages that display a subset of a
Is it possible to obtain a reference/pointer to a class type and enforce that
I have an arraylist that holds a subset of names found in my database.
I have a small subset of fields that need blanked on a page (so
I have the following Java ME code that I'd like to port to BlackBerry:
I have a relatively simple subset of tables in my database for tracking something

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.