I use an empty namespace definition to simplify declarations for recursive functions, where one "side" is operator overloads. The operators are placed in their own namespace to allow selective use in scopes as desired, rather than forcing use if the header is included anywhere (and thus forcing errors if resolutions become ambiguous).
Example:
namespace container_inserters {}
template<class Stream, class Iter, class Ch>
void write_sequence(Stream& s, Iter begin, Iter end,
Ch const *initial, Ch const *sep, Ch const *final)
{
using namespace container_inserters;
if (initial) s << initial;
if (begin != end) {
s << *begin;
++begin;
for (; begin != end; ++begin) {
if (sep) s << sep;
s << *begin;
}
}
if (final) s << final;
}
namespace container_inserters {
#define G(N) \
template<class Ch, class Tr, class T, class A> \
std::basic_ostream<Ch,Tr>& operator<<(std::basic_ostream<Ch,Tr> &s, \
N<T,A> const &value) \
{ \
write_sequence(s, value.begin(), value.end(), "[", ", ", "]"); \
return s; \
}
G(std::deque)
G(std::list)
G(std::vector)
#undef G
} // container_inserters::
The resolution of s << *begin
is delayed until write_sequence is instantiated (because it involves template parameters), by which time the operator has been declared and can be found through the using directive. The call becomes recursive for nested containers:
int main() {
using namespace std;
vector<deque<list<int> > > v (3, deque<list<int> >(2, list<int>(1, 42)));
using namespace container_inserters;
cout << v << '\n';
return 0;
}
// output:
// [[[42], [42]], [[42], [42]], [[42], [42]]]
Boost has a similar output formatting library, but I don't know if they use the same implementation technique.