Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-04-07 18:56:53


AMDG

Scott McMurray wrote:
> 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.
>

In this case you can put operator<< in the namespace of
space_separated_formatter,
where it will be found by ADL. It's completely legal then.

In Christ,
Steven Watanabe


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