I've written an overload of ostream operator<< for std::vector. It's based on a solution found somwhere here, in the depths of stackoverflow:
template <typename T, typename AllocT>
std::ostream& operator<< (std::ostream& out, const std::vector<T, AllocT>& v) {
if (!v.empty()) {
out << "[";
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
out << "\b\b]";
}
return out;
}
Everything seems to work perfectly fine with simple types:
auto vt = std::vector<int>({ 1, 2, 4 });
std::cout << vt;
>> [1, 2, 4]
However, whenever I try to make this recursive:
auto vt = std::vector<std::vector<int>>({ {1, 2}, {3, 4} });
std::cout << vt;
>> error C2679: binary '<<': no operator found which takes a right-hand operand of type 'const _Ty' (or there is no acceptable conversion)
with
[
_Ty=std::vector<int,std::allocator<int>>
]
... while trying to match the argument list '(std::basic_ostream<char,std::char_traits<char>>, const _Ty)'...
... while compiling class template member function 'std::ostream_iterator<T,char,std::char_traits<char>>
&std::ostream_iterator<T,char,std::char_traits<char>>::operator =(const _Ty &)'
with
[
T=std::vector<int,std::allocator<int>>,
_Ty=std::vector<int,std::allocator<int>>
]
It's quite strange, since the complier seem to be stuck on resolving overload for operator<<(std::basic_ostream<char,std::char_traits<char>>, const std::vector<int,std::allocator<int>>&)
, which (in theory) we should be able to produce.
As far as I understand, std::ostream
is actually the same as std::basic_ostream<char>
, so there should not be any problems here. Or maybe something's wrong with constness, though I also can't see the actual problem.