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

  • Home
  • SEARCH
  • 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 8370443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:54:50+00:00 2026-06-09T13:54:50+00:00

Is it at all possible to use Type Erasure to create objects that encapsulate

  • 0

Is it at all possible to use Type Erasure to create objects that encapsulate an arbitrary type, (let’s call it ErasedType), and can be queried at runtime to tell whether another arbitrary type T is convertible to ErasedType?

After thinking about it, I don’t think it’s possible – even though it seems it could potentially be possible in theory. The compiler would know which types T we’re trying to compare with ErasedType, and so could generate the necessary code before runtime. The problem is that, in practice, there doesn’t seem to be ANY way to pass a template parameter type from a Base class instance to a Subclass instance.

For example:

struct FooBase
{
    template <class TestType>
    bool is_convertible()
    {
        return call_derived();
    }

    protected:

    virtual bool call_derived() = 0;

    template <class ErasedType>
    void base_class_function() { }
};

template <class ErasedType>
struct Foo : public FooBase
{
    bool call_derived()
    {
        // Here we have access to the ErasedType but no access to TestType.
            //
        // We could pass ErasedType to a base class function by saying:
        //
        // this->base_class_function<ErasedType>();
        //
        // ...but that doesn't seem to help since we still don't have access to
        // TestType
    }
};

So, the goal is to be able to say something like:

FooBase* f = new Foo<int>();
bool res1 = f->is_convertible<double>(); // returns true
bool res2 = f->is_convertible<long>(); // returns true
bool res3 = f->is_convertible<std::string>(); // returns false

But, I can’t see how the FooBase::is_convertible method could ever be implemented, since I see no way to make TestType and ErasedType accessible together, in the same function, so the compiler could compute the result of std::is_convertible<TestType, ErasedType>::value

So, is this at all possible?

  • 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-09T13:54:51+00:00Added an answer on June 9, 2026 at 1:54 pm

    It is indeed not possible in C++, in general. It takes quite a bit of meta-data to make arbitrary queries about types at runtime, and C++ tries to keep this minimal (sometimes to the point of being a bit annoying; a feature could be automatically opt-in “on use”, so there’s no unnecessary overhead, but I disgress).

    As David has been alluding to, it’s entirely possible to duplicate the compilers information up to a point, but never fully automatically. This limits the runtime type information to what you manually add.

    Take a look at libraries like Qt that have an entire framework on top of C++ to provide this meta-data to see what kind of work is involved. Depending on the problem at hand, you may be able to get by without it.

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

Sidebar

Related Questions

Well, question is in title. Is it possible at all? I can use css
I am having trouble correctly modeling related objects that can use templates. This is
Welcome all It is possible to use SimpleXML for android in commercial apps? THis
Is it possible to use all features of Linq to SQL for a MySQL
Is it at all possible to create a static/tv interference/twitch effect similar to what
How can I enumerate all possible instances of System.Char ? I need to see
Does Dns.GetHostAddresses really get back all registered IPs? That is DNS can have multiple
I have code that gives a list of all possible values for any given
Suppose I've got a generic MyClass<T> that needs to compare two objects of type
I have tried all the possible options to use openID delegation on my site,

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.