I'm attempting to create a generic container type to provide a single common interface, as well as hide the internal containers I'm using as they are subject to change.
Basically I have plugins that return collections of items and I don't wish the plugins to be aware of the type of container my code is using.
Can anyone point me in a better direction then the example code below?
template<class C, typename I> class Container { public: //... void push(const I& item) { if(typeid(C) == typeid(std::priority_queue<I>)) { std::priority_queue<I>* container = (std::priority_queue<I>*)&_container; container->push(item); } if(typeid(C) == typeid(std::list<I>)) { std::list<I>* container = (std::list<I>*)&_container; container->push_back(item); } else { //error! } }; private: C _container; //... } Thanks
list,deque, orvector(or any other nonstandard container that meets the sequence container requirements) without any special handling at all. The entire point of the container concepts is that you can easily write code that will work with any container that meets the requirements. (Yes, this means you won't be able to use non-containers likepriority_queuedirectly, but it's highly unusual to need to do that anyway.)