|
Boost Users : |
From: Andy Stevenson (andy.stevenson_at_[hidden])
Date: 2008-04-07 18:59:08
Scott,
Thanks. What you're proposing is an elegant work around.
I posted separately but it leaves me feeling I'm missing something about
extensibility that the std/boost authors had in mind.
Regards, Andy
-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Scott McMurray
Sent: 07 April 2008 23:10
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] [boost][lexical_cast] simple example doesn't
workwith vector<T>
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 mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
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