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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:13:59+00:00 2026-06-13T14:13:59+00:00

Suppose you have a class like template<class T> struct A { void foo() {

  • 0

Suppose you have a class like

template<class T>
struct A {
  void foo() {
    // Need access to "T" here
    typedef typename someTrait<T>::someType T2;
  }
};

and you would like to “register” (or store) instances of the class (or a pointers to it) with a container (probably STL) for later calling the foo() method of all registered instances.

Since instances instantiated with different template parameters are to be stored (A<int>, A<float>, …) obviously one can’t use a std::vector and store the instances or pointers to it. What I can imagine is making the method static and storing function pointers to void foo(), like:

 void static foo();

 typedef void (* fooPtr)(void);
 std::vector<fooPtr>

But somehow I have the feeling this is not very C++11-ish. Is there a nice solution which introduces a wrapper class or something?

Introducing a base class and using dynamic cast would introduce dependencies on RTTI, right? I would like to avoid dependencies on RTTI.

How would one do this in C++11? (I would not like to introduce additional dependencies like linking to Boost or depending on RTTI.)

Thanks for your opinions!

  • 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-13T14:14:01+00:00Added an answer on June 13, 2026 at 2:14 pm

    Probably you could just use virtual methods? This works with C++03 too.

    struct ABase {
        virtual void foo() = 0;
    };
    
    template<class T>
    struct A : ABase {
        virtual void foo() override {
            // Access to "T" here
        }
    };
    
    ...
    
    std::vector<std::unique_ptr<ABase>> vec;
    vec.emplace_back(new A<int>());
    vec.emplace_back(new A<float>());
    
    for (auto& p_a : vec)
        p_a->foo();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
Suppose a construct like this: class Interface { public: template <typename T> virtual void
Suppose I have something like this: struct EmptyClass{}; template<typename T1, typename T2 = EmptyClass,
Suppose I have the following class template: template<typename T> struct Wrapper { T* t_;
Suppose you have: template<class T> class A { template<class T1> void foo(const T1& t1)
Suppose I have a function which looks like this: template <class In, class In2>
Suppose you have a simple class like this: class foo{ private: int* mData; int
Let's suppose I have a css class named foo. I would like to have
Suppose I have an autolocker class which looks something like this: template <T> class
Suppose I have following template class: template<unsigned char I, unsigned char F> class FOO

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.