I am trying to figure out why the following piece of code, where a template of stream output operator function is written, requires using template template parameters:
https://wandbox.org/permlink/W85pV5GhVzI95b3e
#include <iostream>
#include <vector>
#include <deque>
#include <list>
template<template <class> class C, class T>
std::ostream& operator <<(std::ostream& os, const C<T>& objs)
{
for (auto const& obj : objs)
os << obj << ' ';
return os;
}
int main()
{
std::vector<float> vf { 1.1, 2.2, 3.3, 4.4 };
std::cout << vf << '\n';
std::list<char> lc { 'a', 'b', 'c', 'd' };
std::cout << lc << '\n';
std::deque<int> di { 1, 2, 3, 4 };
std::cout << di << '\n';
return 0;
}
Why can't I just write the template of the operator function like this:
template <class T>
std::ostream& operator <<(std::ostream& os, T& objs) {...}
In this case I get a lot of errors saying: error: ambiguous overload for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream'} and 'const char') os << obj << ' ';
Can someone help me understand this?