Consider the following overloaded function that can print a 1d vector and a vector of vector of several types like strings, ints, doubles etc.
template<typename T>
void p(const vector<vector<T>>& vec) {
int count = 0;
for (vector<T> innerVec: vec) {
cout << count++ << ": ";
for (T e :innerVec) {
cout << e << ' ';
}
cout << '\n';
}
cout << '\n';
}
template<typename T>
void p(const vector<T>& vec) {
for (T e: vec) {
cout << e << ' ';
}
cout << '\n';
}
Is there anyway I can merge these two functions into 1? I tried using SFINAE and tag dispatching but all the solutions I could come up with need a macro or multiple functions and I don't want this.
I know the question might seem odd since my solution works, but I prefer having just one function in my code. This is because I want to implement a function that can detect if I am passing in a map, vector, vector of vectors, unordered_set, multimap, etc and just print that STL data structure and having one overloaded function for each specialization is a bit annoying as it gets large quick.