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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:03:00+00:00 2026-06-13T03:03:00+00:00

I am trying to create a generic container which can store heterogeneous objects of

  • 0

I am trying to create a generic container which can store heterogeneous objects of type Wrapper< T> where T can be any user defined type. I have seen boost::any and other solutions, but i cant call the function foo() without recasting(i dont know which type to recast,information about T is lost.) it back to original type.

How can I reasonably implement a Generic container/use existing generic container to achieve this ?

template <typename T>
class Wrapper{
public:
  Wrapper(const T& a):o(a){};
  Wrapper(){};
  //public methods
  void foo(){
     //do stuff
  };
private:
 T o;
};

class X{};
class Y{};

int main(){
  X x;
  Y y;

  A_GENERIC_CONTAINER generic_container;
  // A_GENERIC_CONTAINER should be able to store
  // any number of heterogeneous objects of type Wrapper<T>
  // where T can be any user defined type.

  generic_container.push_back(x);
  generic_container.push_back(y);

  auto it =  generic_container.begin();
  auto end =  generic_container.end();
  while(it != end){
    it->foo();
    ++it;
  }
}
  • 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-13T03:03:00+00:00Added an answer on June 13, 2026 at 3:03 am

    The most generic way is to create a base class for Wrapper, say BaseWrapper, and define the pure virtual functions on the BaseWrapper that then are implemented on each of the Wrapper classes. Through specialization, each Wrapper, Wrapper, could have their own implementation. Once you’ve done that, you can use a container of a smart pointer to the Base Type, and use it at your leisure.

    There are some possible shortcuts. For example, in the simple case in your example, I would recommend using std::function. A helper function aids in doing this. Note, this really only works if there is only 1 method to be called. I also would recommend having your wrapper define operator() directly, so that you don’t have to use the bind.

    template<T>
    std::function<void()> wrap(const T& x)
    {
       return std::bind(&Wrapper<T>::foo, Wrapper<T>(x));
    }
    
    int main(){
      X x;
      Y y;
    
      std::vector<std::function<void()> > generic_container;
      // A_GENERIC_CONTAINER should be able to store
      // any number of heterogeneous objects of type Wrapper<T>
      // where T can be any user defined type.
    
      generic_container.push_back(wrap(x));
      generic_container.push_back(wrap(y));
    
      auto it =  generic_container.begin();
      auto end =  generic_container.end();
      while(it != end){
        (*it)();
        ++it;
      }
    }
    

    Edit:

    What I was discussing was a non-templated base, from which you derive all of your templated wrappers. This allows you to call the methods you have pre-defined (and that must be implemented by the Wrapper), without knowing the specific wrapper type involved.

    class Base
    {
      public:
       virtual ~Base() {};
       virtual void foo() = 0;
    };
    
    template <typename T>
    class Wrapper : public Base{
    public:
      Wrapper(const T& a):o(a){};
      Wrapper(){};
      //public methods
      virtual void foo(){
         //do stuff
      };
    private:
     T o;
    };
    
    int main(){
      X x;
      Y y;
    
      std::vector<std::shared_ptr<Base> > generic_container;
      // A_GENERIC_CONTAINER should be able to store
      // any number of heterogeneous objects of type Wrapper<T>
      // where T can be any user defined type.
    
      generic_container.push_back(std::make_shared<Wrapper<X>>(x));
      generic_container.push_back(std::make_shared<Wrapper<Y>>(y));
    
      auto it =  generic_container.begin();
      auto end =  generic_container.end();
      while(it != end){
        it->foo();
        ++it;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a generic container which has a copy-constructor. I'm having trouble
I am trying to create a generic container (e.g. java code below), which has
Trying to create a generic video plugin for Expression Engine 2 where I can
I am trying to create a generic formatter/parser combination. Example scenario: I have a
I am trying to create a generic class which new's up an instance of
I'm trying to create a generic graphics export tool which works by implementing the
I'm trying to create a generic LINQ-TO-SQL repository based on this post which basically
I have an interface which takes a generic type interface IIFace<T> Then i have
Using C++, I'm trying to create a generic container class to handle multiple data
I'm trying to create a generic method where the type is a generic interface.

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.