Okay, so I’m just learning about templates. Anyways, I’m probably (most definitely) doing something wrong, but here’s the problem:
My first template function is declared like this:
template<typename T>
std::ostream& printFormatted(T const& container, std::ostream& os = std::cout) {
//...
}
Then I’m supposed to implement a specialized case for maps, so this is what I tried to do:
template<>
std::ostream& printFormatted<std::map<typename key, typename value>>(std::map<typename key, typename value> const& container, std::ostream& os = std::cout) {
//...
}
I might be making a mistake with my key/value variables, not sure, but regardless, when trying to compile I get the error message:
error: wrong number of template arguments (1, should be 4)
error: provided for ‘template<class _Key, class _Tp, class _Compare, class _Allocator> class std::__debug::map’
Clearly there’s something I don’t understand about templates or maps? Someone please help?
Assuming your uses of
keyandvalueare meant to be placeholers, you cannot declare template parameters inline with the keywordtypename. That is to say,Foo<typename T>is always invalid — but not to be mistaken withFoo<typename T::bar>which is different altogether. The syntax for specialization looks like:but that wouldn’t work because it’s a partial specialization and those are not allowed for function templates. Use overloading instead:
This overload will be preferred over the more general template.