How to store templated objects in an STL container and member function call
c++, c++11, pointer-to-member, stl, templates
Solution
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();
Problem
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!