That's a hard one for me. I need to store different classes with same template member function in one container. The member function is always the same declaration.
struct foo_creator {
template < class T >
base* create() {
return new foo<T>;
}
};
struct bar_creator {
template < class T >
base* create() {
return new bar<T>;
}
};
The functions are applied to multiple types. At time of storage I don't know the exact types on which I want to apply the functions. That's way I can't work with a base class.
Is there a way to store them in one container or at least something that let me apply the functions later when I know the concrete type?
Because it was requested I'll mockup the real use case. I've created a container for different types of objects. To make that work the user need to introduce the types to the container. But I also want to be notified when a type is introduced, inserted, deleted and so on.
So I decided to add an observer (not knowing for which type it is called).
struct container
{
// register an observer
void register_observer(observer *o) { // ... }
// introduce the type container can store
template < class T >
void introduce(const char *name)
{
T prototype;
// observer should be called for new type
o->notify_introduce_new_type(prototype);
}
template < class T >
void insert(T *t) { // ... }
}
int main()
{
store s;
s.register_observer(new printer_observer);
s.introduce<Foo>("foo") // notification
s.introduce<Bar>("bar") // notification
s.insert(new Foo); // notification
}
I afraid that couldn't be solvable.
Anyway thanks in advance!