"Delfin Rojas"
Hello all,
I hope somebody can help me with this. What I want to do is the following:
Let's say I have a templated class called Foo
template<class U> class Foo { public: Foo(U * p) : m_p(p) {} ~Foo() {} U* getP() const { return m_p; } private: U* m_p; };
Now let's say I want to have a collection of pointers to Foo. What should I do? The possibilities I can think of are the following:
1) Create a class FooBase and have Foo inherit from FooBase. FooBase is non-templated so there should be no problem with having a collection of pointers to FooBase where I can put instances of Foo. The problem with this approach is that given a FooBase* in that collection I could not use the getP method because I would not know what type of Foo it holds.
Not knowing what type of Foo it holds, how would you know what to do with the result of getP? Its type depends on the type of the Foo. If you are willing to just get a void*, type erasure (see http://www.boost.org/doc/html/signals/s05.html#id574178 and http://www.boost-consulting.com/mplbook) might help. Otherwise...
2) Create a boost::variant of all the possible types of Foo<U>* I want to have. Then have a collection of this FooVariant. Using static visitation I can obtain the original type of Foo<U>* in a given FooVariant. The problem with this approach is that I get an ICE from VC++ 7.1 when the type of U gets too complex (shared_ptr to another type for example).
Have I overlooked something? Is there a better technique for what I want to achieve? Any help would be great,
You could store them in shared_ptr<void>, or use boost::any. -- Dave Abrahams Boost Consulting www.boost-consulting.com