However, you cannot overload operator<< to do this. Since containers have no common base class, the only way to do this is using templates, and the above template declaration matches basically any type. Doing the above with operator<< would make operator<< ambiguous for any type with its own operator<<, and produce some seriously weird errors.

If you insist on overloading operator<<, I think the solution using boost::enable_if giving on SO is just about the best you can do.