Option number 1
Use tag sending:
template <typename T> struct tag {}; template <typename container> class A { private: container m_container; template <typename T, typename Alloc> void foo_spec(tag<std::vector<T, Alloc> >) {
Demo 1
Option number 2
Partially specialize a separate class with a static member function:
template <typename T> struct Impl; template <typename T, typename Alloc> struct Impl<std::vector<T, Alloc> > { static void foo_spec() {
Demo 2
Option number 3
Derive from a partially specialized class + CRTP idioms:
template <typename container> class A; template <typename CRTP> struct Base; template <typename T, typename Alloc> struct Base<A<std::vector<T, Alloc> > > { void foo() {
Demo 3
Option number 4
Use the reverse inheritance hierarchy proposed by David Rodriguez - dribeas in the comments:
template <typename container> class Base { private: container m_container; public: // more generic methods that work with any container }; template <typename container> class A; template <typename T, typename Alloc> class A<std::vector<T, Alloc> > : public Base<std::vector<T, Alloc> > { public: void foo() { // vector specific implementation } }; template <typename K, typename V, typename C, typename Alloc> class A<std::map<K,V,C,Alloc> > : public Base<std::map<K,V,C,Alloc> > { public: void foo() { // map specific implementation } };
Demo 4
Piotr skotnicki
source share