问题
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:
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();
回答2:
If you actually need an array of functions, you can use std::function
with conjunction to std::bind
std::vector<std::function<void()> vec;
A<int> a;
vec.push_back(std::bind(&A<int>::foo, &a)); //now a should live at least as long, as vec
// or vec.push_back(std::bind(&A<int>::foo, a)); - now a will be _copied_ to vec
foo
now is a memeber function (std::bind
'binds' the function to given variable here)
来源:https://stackoverflow.com/questions/13065271/how-to-store-templated-objects-in-an-stl-container-and-member-function-call