I have a function that prints generic container as following:
template<typename T>
void printContainer(std::ostream& out, const T& container)
{
std::copy(std::begin(container),
std::end(container),
std::ostream_iterator<typename T::value_type>(out, " "));
}
This function works fine for vector, list, set, etc. However, for map it doesn't work because there is not operator<< for pair. So I added the following overloading of operator<<:
template <typename Key, typename Value>
ostream& operator<<(ostream& out, const pair<const Key, Value>& element)
{
return out << "(" << element.first << "," << element.second << ")";
}
The following discussion also suggest that this is a legitimate solution: link to relevant post
However, I am getting the following compilation error:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'const std::pair<const _Kty,_Ty>' (or there is no acceptable conversion)
I am looking at the type that the compiler claims is missing and at my implementation of operator<< for pair and they seem identical.
I tried to move operator<< code to hpp file, remembering that compilers tend to see one cpp file at time, but it didn't help.
Any suggestions would be appreciated...