Problem with specializing templates
What I am trying to do is to detect automatically whether a container contains simple value_types or key-value pairs (whether the container is map or similar or not). For that I am hoping to instantiate a specialized template for containers whose value_type is a std::pair).
However, the pair specialization is not picked up, unless I specifically state the pair with its template arguments. (The int specialization is just to check that it in principle should work.)
What could I do to make maps automatically detected?
Code:
#include <vector>
#include <list>
#include <map>
#include <iostream>
template <class Cont, class ValueType = typename Cont::value_type>
struct S
{
void print() { std::cout << "General\n"; }
};
template <class Cont>
struct S <Cont, int >
{
void print() { std::cout << "Special: int\n"; }
};
template <class Cont>
struct S <Cont, std::pair<typename Cont::key_type, typename Cont::mapped_type> >
{
void print() { std::cout << "Special: map\n"; }
};
int main()
{
S<std::list<int> > s1;
S<std::vector<double> > s2;
S<std::map<int, double> > s3;
S<std::map<int, double>, std::pair<int, double> > s4;
S<std::map<int, double>, std::map<int, double>::value_type > s5;
s1.print(); //Special: int OK
s2.print(); //General OK
s3.print(); //General not OK, expected Special: map
s4.print(); //Special: map OK
s5.print(); //General not OK
}