|
Boost Users : |
From: Scott McMurray (me22.ca+boost_at_[hidden])
Date: 2008-04-07 18:09:35
On Mon, Apr 7, 2008 at 3:26 PM, Andy Stevenson
<andy.stevenson_at_[hidden]> 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...
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net