Is it possible to make compiler choose between template specifications depending on type traits? For example, consider two template implementations of a Compare functional, one for sequential types (string
s, vector
s, list
s, etc.) and another one for integer types. Can we have only one template specialization for the each class of types?
template <class SeqT>
class Compare
{
public:
bool operator()(const SeqT& s1, const SeqT& s2) const
{
typename SeqT::const_iterator it1=s1.begin();
typename SeqT::const_iterator it2=s2.begin();
while(it1!=s1.end() && it2!=s2.end())
{
if(*it1<*it2) return true;
if(*it2<*it1) return false;
++it1; ++it2;
}
return it2!=s2.end();
}
};
template <class IntegerT>
class Compare
{
public:
bool operator()(IntegerT i1, IntegerT i2) const
{
return i1<i2;
}
};
template <class T, class Cmp = Compare<T> >
class SomeContainer
{
...
};
Basically, what I am looking for is a way to partially specialize a template by imposing a condition on the template argument. Like the first Compare<>
specialization should be applied to the following types: std::basic_string<>
, std::vector<>
, std::list<>
, and the second for the following types: int
, unsigned
, short
, char
. Is that possible?