
On Mon, Apr 7, 2008 at 3:26 PM, Andy Stevenson <andy.stevenson@uk.fujitsu.com> wrote:
The suggestions were to put it into namespace std or boost, both seem to work by the way.
However what is actually right here? Extending std seems distinctly non-std !
I really don't like either of them. Putting it in std:: is non-standard, and unsafe in practice as it's quite plausible that many people would want to give an implementation for it. Putting it in boost:: isn't really satisfactory either, since then it won't be simple to call in your normal code. Perhaps do something like this: template <typename T> struct space_separated_formatter { T &c; space_separated_formatter(T &c_) : c(c_) {} }; template <typename T> space_separated_formatter<T const> space_separated(T const &c) { return space_separated_formatter<T const>(c); } namespace std { template <typename T> ostream &operator<<(ostream &sink, space_separated_formatter<T> const &c) { copy( c.begin(), c.end(), ostream_iterator<typename T::value_type>(sink, " ") ); return sink; } } then boost::lexical_cast<std::string>( space_separated(myvec) ) or std::cout << space_separated(myvec); Still perhaps illegal, but probably safe in practice, especially if you wrap the type up into a namespace, since it'd make collisions unlikely. But whether it'd be worth it is a whole other story...