You can use the overload and send tags mechanism:
#include <vector> template <class T> struct Tag { }; template<typename ContainerType> class ConfIntParamStat { public: typedef typename ContainerType::value_type Type; //... // private: void sample(int iteration) { sample_impl(Tag<ContainerType>(), iteration); } template <class T> void sample_impl(Tag<std::vector<T> >, int iteration) { //if vector } template <class T> void sample_impl(Tag<T>, int iteration) { //if not a vector } }; int main() { ConfIntParamStat<std::vector<int> > cips; cips.sample(1); }
As skypjack said, this approach has little appeal when using const. If you are not using c++11 (I suspect you are not using it because you use the > > syntax for nested templates), you can work around this as follows:
#include <iostream> #include <vector> template <class T> struct Tag { }; template <class T> struct Decay { typedef T Type; }; template <class T> struct Decay<const T> { typedef T Type; }; template<typename ContainerType> class ConfIntParamStat { public: typedef typename ContainerType::value_type Type; //... // private: void sample(int iteration) { sample_impl(Tag<typename Decay<ContainerType>::Type>(), iteration); } template <class T> void sample_impl(Tag<std::vector<T> >, int iteration) { std::cout << "vector specialization" << std::endl; } template <class T> void sample_impl(Tag<T>, int iteration) { std::cout << "general" << std::endl; } }; int main() { ConfIntParamStat<const std::vector<int> > cips; cips.sample(1); }
source share