I'm able to define and use:
std::ostream& operator<<(std::ostream& os, std::vector<int> const& container)
{
for (auto const& n : container)
os << n << ", ";
return os;
}
int main()
{
std::vector<int> data{0,1,2};
std::cout << data << '\n';
}
(demo)
But the definition of that operator doesn't depend on what kind of container I use. From there, I'd like to define a templated version:
template<class Iterable>
std::ostream& operator<<(std::ostream& os, Iterable const& iterable)
{
for (auto const& n : iterable)
os << n << ", ";
return os;
}
int main()
{
std::vector<int> data{0,1,2};
std::cout << data << '\n';
}
(demo)
This is where my compiler gets angry and verbosily reject it:
error: ambiguous overload for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'char')
... with a lot of possible candidates.
Why is it not legal and how could I define such an operator?