If I have a type T, then what is a useful way to check it at compile time to see if it is an STL style container (for an arbitrary value type) or not?
(Assumption: pointers, links, etc. Already divided)
Starting code:
template<class T> // (1)
void f(T&) {}
template<class T> // (2)
void f(std::vector<T>&) {}
void test()
{
int a;
std::vector<int> b;
f(a);
f(b);
}
Now this works fine, but what if I want to generalize the container (i.e. not define (3), (4), ... explicitly)?
Using SFINAE and type lists will slightly reduce the code, but is there a better way?
Or is there an idiom for concept-based specialization?
Or could I somehow use SFINAE to selectively include only the desired specializations?
, - , T .
MSalters:
template<class T>
void f(T&, ...) {
std::cout << "flat" << std::endl;
}
template<class Cont>
void f(Cont& c, typename Cont::iterator begin = Cont().begin(),
typename Cont::iterator end = Cont().end()) {
std::cout << "container" << std::endl;
}
( , f )