
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? - Volodya

On Wed, 30 Mar 2005 17:35:12 +0400, Vladimir Prus <ghost@cs.msu.su> 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. 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.
Is the free-function syntax str (format (...)) more palatable? -- Caleb Epstein caleb dot epstein at gmail dot com

Caleb Epstein wrote:
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.
Is the free-function syntax
str (format (...))
more palatable?
No, it's still too inconvenient for me. - Volodya

From: Vladimir Prus <ghost@cs.msu.su>
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.
While your usage might be proven normative and thus could be used to justify adding an implicit conversion, how about another approach? Why not add a non-member function str() or to_string(): template <class T> std::string boost::str(T const & source_i) { return source_i.str(); } Then, you example becomes: s = str(format("%1%") % 10); (I don't even know the types involved in your expression to make a non-templated function. Besides, I thought this might be generally useful.) To generalize further (warning: uncompiled, untested code): template <class T> typename boost::result_of<&T::str()>::type boost::str(T const & source_i) { return source_i.str(); } That would be more flexible. This could also be the primary template specialization and it could be specialized for other types that don't provide a str() member function. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

On Wed, 30 Mar 2005 11:07:08 -0500 (EST), Rob Stewart <stewart@sig.com> wrote:
While your usage might be proven normative and thus could be used to justify adding an implicit conversion, how about another approach? Why not add a non-member function str() or to_string():
The free-function "str" already exists: http://boost.org/boost/format/free_funcs.hpp -- Caleb Epstein caleb dot epstein at gmail dot com

From: Caleb Epstein <caleb.epstein@gmail.com>
On Wed, 30 Mar 2005 11:07:08 -0500 (EST), Rob Stewart <stewart@sig.com> wrote:
While your usage might be proven normative and thus could be used to justify adding an implicit conversion, how about another approach? Why not add a non-member function str() or to_string():
The free-function "str" already exists:
Well, there you go. I don't use format, and I assumed Vladimir would have known about and mentioned such a function if it existed. Once again, it is proven that one should not assume! -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Rob Stewart wrote:
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.
While your usage might be proven normative and thus could be used to justify adding an implicit conversion, how about another approach? Why not add a non-member function str() or to_string():
template <class T> std::string boost::str(T const & source_i) { return source_i.str(); }
For format, such function already exists. However, it would be nice to have it fully generic -- just a more conveniently spelled synonym for lexical_cast. "lexical_cast" is just too long for a commonly used and no-so-dangerouns (unlike, say reinterpret_cast), functionality.
Then, you example becomes:
s = str(format("%1%") % 10);
(I don't even know the types involved in your expression to make a non-templated function. Besides, I thought this might be generally useful.)
To generalize further (warning: uncompiled, untested code):
template <class T> typename boost::result_of<&T::str()>::type boost::str(T const & source_i) { return source_i.str(); }
That would be more flexible. This could also be the primary template specialization and it could be specialized for other types that don't provide a str() member function.
Or the other way around. The generic version will use stream io, and specific version will call .str() - Volodya

On Wed, 30 Mar 2005 17:35:12 +0400, Vladimir Prus <ghost@cs.msu.su> 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
participants (4)
-
Caleb Epstein
-
Maxim Yegorushkin
-
Rob Stewart
-
Vladimir Prus