Boost logo

Boost :

From: Maxim Yegorushkin (e-maxim_at_[hidden])
Date: 2005-04-01 02:43:37


On Wed, 30 Mar 2005 17:35:12 +0400, Vladimir Prus <ghost_at_[hidden]> wrote:

>
> Hello!
> While it's generally considered a bad practice to provide implicit
> conversions, I think it would be good to provide such a conversion for
> the
> boost::format class. Currently, I can't write:
>
> string s;
> s = format("%1%") % 10;
>
> and need to write
>
> s = (format("%1%") % 10).str();
>
> Maybe it's specifics of my code, but I can count 10 such conversions in a
> single source file. I think that:
>
> 1. Calling .str() is rather inconvenient.
> 2. The 'format' class most often used inside expressions. It's not
> likely to
> be passed around to functions and so the dangers of implicit conversions
> are
> not critical.
>
> Thoughts?

As a shortcut you can overload any available operator you like. Binary one
for string and format or unary for format. The latter requires additional
coding or additional parenthesis but has an advantage of being able to be
used as an initializer. Something like that:

// binary operator
string s;
s << format("%1%") % 10;

// unary operator
string s(~(format("%1%") % 10));

-- 
Maxim Yegorushkin

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk