J
John Harrison
Is the following code legit?
#include <iostream>
#include <utility>
#include <map>
#include <algorithm>
#include <iterator>
namespace std
{
template <class T, class U>
std:stream& operator<<(std:stream& out, std:air<T, U> const& p)
{
return out << p.first << ' ' << p.second;
}
}
int main()
{
std::map<int, int> m;
std::copy(m.begin(), m.end(),
std:stream_iterator<std::map<int, int>::value_type>(std::cout));
}
No less than three compilers accept this (VC++ 7.1, g++ 3.3.1 and online
Comeau C++) but it seems that the standard doesn't like it, 17.4.3.1 para
1 allows template specialisations to be added to std, but not it seems
function overloads.
The same three compilers do not accept the same code but with operator<<
defined in the global namespace. All three give variations on 'no suitable
operator<< found' error messages.
So it is possible to define operator<< for a type in the std namespace. If
so how to do it?
John
#include <iostream>
#include <utility>
#include <map>
#include <algorithm>
#include <iterator>
namespace std
{
template <class T, class U>
std:stream& operator<<(std:stream& out, std:air<T, U> const& p)
{
return out << p.first << ' ' << p.second;
}
}
int main()
{
std::map<int, int> m;
std::copy(m.begin(), m.end(),
std:stream_iterator<std::map<int, int>::value_type>(std::cout));
}
No less than three compilers accept this (VC++ 7.1, g++ 3.3.1 and online
Comeau C++) but it seems that the standard doesn't like it, 17.4.3.1 para
1 allows template specialisations to be added to std, but not it seems
function overloads.
The same three compilers do not accept the same code but with operator<<
defined in the global namespace. All three give variations on 'no suitable
operator<< found' error messages.
So it is possible to define operator<< for a type in the std namespace. If
so how to do it?
John