Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost.Format to a string
From: Szymon Gatner (szymon.gatner_at_[hidden])
Date: 2011-06-26 19:40:02


2011/6/27 John M. Dlugosz <mpbecey7gu_at_[hidden]>:
> I brought this up a while back, and I ran into it again yesterday.  Using
> boost::format to produce a string, as opposed to sending it directly to
> cout, is awkward.
>
>    boost::format ("blah blah blah") % exp1 % exp2 %exp3
>
> is fine in and of itself.  But then what?  You need to wrap the whole thing
> in extra parens to use str:
>
>    std::string s= (boost::format ("blah blah blah") % exp1 % exp2
> %exp3).str();
>
>
> WHY NOT allow implicit conversion to string?
>
> Or, some other mechanism that doesn't complicate the expression and bury the
> interesting part?

use str() free function:

std::string s = str(boost::format ("blah blah blah") % exp1 % exp2 %exp3)

same when passing format to functions taking std::string params

Szymon


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