RE: [boost] [format] conversion to string

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. [snip] Having been bit far too many times by bad implicit conversions, I would vote against such a change. I don't see why one shouldn't pass in a format object to a function (possibly for the function to add one argument to the format object).
I still think that providing a member function with(), rather than an operator % would have been better. Then one could write: string s = format("%1%").with(10).str(); which seems does seem neater than: string s = (format("%1%") % 10).str(); If we are to change boost::format, I would prefer to add such a member function than an implicit conversion. -- Martin Bonner Martin.Bonner@Pitechnology.com Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ, ENGLAND Tel: +44 (0)1223 441434

Martin Bonner wrote:
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. [snip] Having been bit far too many times by bad implicit conversions, I would vote against such a change. I don't see why one shouldn't pass in a format object to a function (possibly for the function to add one argument to the format object).
And for implicit conversion to do any harm, you need to mistakenly pass boost::format to a function which takes std::string, right? In this case, boost::format will immediately throw and you'll detect the bug in no time.
I still think that providing a member function with(), rather than an operator % would have been better. Then one could write: string s = format("%1%").with(10).str(); which seems does seem neater than: string s = (format("%1%") % 10).str();
If we are to change boost::format, I would prefer to add such a member function than an implicit conversion.
The 'with' function (or, maybe 'arg', like in QString), can be indeed neater then parenthesis, though I still prefer string s = format("%1%") % 10; - Volodya

Vladimir Prus wrote:
Martin Bonner wrote:
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.
[snip] Having been bit far too many times by bad implicit conversions, I would vote against such a change. I don't see why one shouldn't pass in a format object to a function (possibly for the function to add one argument to the format object).
And for implicit conversion to do any harm, you need to mistakenly pass boost::format to a function which takes std::string, right? In this case, boost::format will immediately throw and you'll detect the bug in no time.
The problem I find more concerning is implicit conversions caused by typo in operators s = format("name=%1 processed") % s1 + s2; if format is convertible to string, the type of s2 here might be such that this line compiles (e.g. s2 of type string), so such typos could go unnoticed for a while.. My opinion was it's safe to use operators for format as long as it is not implictly-convertible. it's true that using a str free function can be a nuisance. -- Samuel

Samuel Krempp wrote:
And for implicit conversion to do any harm, you need to mistakenly pass boost::format to a function which takes std::string, right? In this case, boost::format will immediately throw and you'll detect the bug in no time.
The problem I find more concerning is implicit conversions caused by typo in operators
s = format("name=%1 processed") % s1 + s2;
if format is convertible to string, the type of s2 here might be such that this line compiles (e.g. s2 of type string), so such typos could go unnoticed for a while..
You mean, the human intended to pass two arguments to format, but typed '+' instead of '%'. Then, I think the format string will most probably contain two format specifiers: s = format("name=%1 processed, results is %2%") % s1 + s2; and you'll get an exception on the first execution of this line -- which supposedly will be executed during testing. (BTW, does your example miss '%' after '%1', or I misunderstand something about format specifiesrs).
My opinion was it's safe to use operators for format as long as it is not implictly-convertible.
You can still make mistakes, like omitting paranthesis in: format("reminder = %%") % (a % b) - Volodya

Martin Bonner wrote:
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. [snip] Having been bit far too many times by bad implicit conversions, I would vote against such a change. I don't see why one shouldn't pass in a format object to a function (possibly for the function to add one argument to the format object).
And for implicit conversion to do any harm, you need to mistakenly pass boost::format to a function which takes std::string, right? In this case, boost::format will immediately throw and you'll detect the bug in no time.
I still think that providing a member function with(), rather than an operator % would have been better. Then one could write: string s = format("%1%").with(10).str(); which seems does seem neater than: string s = (format("%1%") % 10).str();
If we are to change boost::format, I would prefer to add such a member function than an implicit conversion.
The 'with' function (or, maybe 'arg', like in QString), can be indeed neater then parenthesis, though I still prefer string s = format("%1%") % 10; - Volodya
participants (3)
-
Martin Bonner
-
Samuel Krempp
-
Vladimir Prus