Once again, I wish C++ had stronger typedefs:
#include <vector>
template<typename T>
struct A {
typedef std::vector<T> List;
};
template<typename T>
void processList(typename A<T>::List list) {
// ...
}
int main() {
A<int>::List list;
processList<int>(list); // This works.
processList(list); // This doesn't.
}
Apparently, the compiler sees list as a std::vector<int> and not an A<int>::List, so it can’t match it against the A<T>::List that is expected.
In the actual case, it’s a longer type name, often repeated, and it’s a nuisance. Apart from letting processList accept a vector instead, is there any way to make template type inference work for me?
No, this is what’s called a non-deducible context.
However, why do you need this anyway? The idiomatic way to pass sequences around is by iterator:
(Note that in your question, you pass a
vectorby value, which is a bad thing to do. For iterators, that’s fine and even to be preferred.)However, if you’re really desperate, you can have the function deduce any container taking a certain number of template arguments:
Note, however, that this would match any template with two template arguments. OTOH, it wouldn’t match a container like
std::map, which has a different number of arguments.