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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:56:51+00:00 2026-05-22T15:56:51+00:00

I was wondering if some form of type erasure exists for dealing with methods

  • 0

I was wondering if some form of type erasure exists for dealing with methods that have the same name and arguments but return different values like in my example below (begin and end). I’m not planning on actually using this anywhere I’m just interested in knowing if it’s possible, and, if so, how it would be done.

The only form of type erasure I know about is having a pointer to a pure virtual concept class which points to a model<T> which forwards calls to the underlying T. However, this requires that all the T‘s contain methods with the exact same signature, while in my example the return types differ. As far as I can tell something akin to virtual template functions would be needed to do what I’m asking, but I may be missing something.

class Iterable
{
    //how would this be defined?
}

int main(int argc, char *argv[])
{
    vector<int> v = {1, 2, 3, 4, 5};
    set<string> s = {"foo", "bar", "baz"};

    Iterable iterable;

    if(argc == 2) iterable = v;
    else iterable = s;


    for(auto val : it)
    { 
        cout << val << ' ';
    }
}
  • 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-22T15:56:51+00:00Added an answer on May 22, 2026 at 3:56 pm

    Type erasure can and has been implemented in C++ in different contexts. The most common approach, which is used in boost::any, std::function< signature >, std::thread and others is based on a non-polymorphic class that is the type erased object, which contains a pointer to an interface type. Internally, during construction, assignment or whenever the user type is erased, an implementation of the interface is instantiated and stored.

    As a motivating simplified example, consider that we wanted to create a printable type that can be used to print any type that implements operator<< to std::cout using type erasure. For that we need the type printable, the internal interface printable_impl_base, and the actual implementations:

    // regular polymorphic hierarchy:
    struct printable_impl_base {
       virtual ~printable_impl_base() {}
       virtual void print() const = 0;
    };
    template <typename T>
    struct printable_impl : printable_impl_base {
       T copy_to_print;
       printable_impl( T const & o ) : copy_to_print( o ) {}
       virtual void print() const {
          std::cout << copy_to_print << std::endl;
       }
    };
    
    // type erasure is performed in printable:
    class printable {
       std::shared_ptr<printablable_impl_base> p;
    public:
       template <typename T>
       printable( T obj ) : p( new printable_impl<T>(obj) ) {}
       void print() const {
          p->print();
       }
    };
    

    Note that the pattern is very similar to a regular polymorphic hierarchy, with the difference that an interface object is added that is a value type (borrowing the term value type from C#), that holds the actual polymorphic objects inside.

    Looking at it this way, it seems kind of simplistic and useless, but that is the fuel that drives boost::any (the internal interface is only a typeid), std::function< void () > (the internal interface is that it implements void operator()), or shared_ptr<> (the interface is the deleter method, that relinquishes the resource).

    There is one specific different type of type erasure when the only thing that needs to be done with the type that implements type erasure is to destroy it: use a temporary and bind it to a constant reference… But this is very specific, if you want you can read about it here: http://drdobbs.com/cpp/184403758

    In the specific case that you are talking about in the question it is a bit more complex, because you don’t want to erase a single type, but rather a couple of them. The Iterable interface must erase the type of the container that it internally holds, and in doing so it has to provide it’s own iterators that have to perform type erasure on the iterators from the container. Still, the idea is basically the same, just more work to do to implement.

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

Sidebar

Related Questions

I'm wondering. If I have a form that is broken up into steps where
I have some form values that include HTML entities, for example: <option value=Coup&#232;> Coup&#232;
Just wondering about some practice about this; I have made a simple visual C#
I have a form page that posts to another page where multiple fields as
I want to make a form type of editor that edits the text right
I have some JSON which I have in a object but I can seem
I'm wondering if I can have an iOS program that stays in a loop,
I have an interesting problem that I'm trying to solve and was wondering if
I have a form that, for example, allows people to enter all their siblings.
I was wondering if there is some standardized way of getting type sizes in

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.