[format] please add operator String()

Hi, In code like this: std::string columns() const { return (boost::format("%7s, %7s, %7s, %7s") % "testCost" % "optimalEcr" % "efficiecyEcr" % "testCostEcr").str(); } it is very annoying to have to put () around the whole expression and then call .str() (Just imagine that we are creating a string on the fly to a function). Therefore I suggest that we add operator string_type() const { return str(); } to the interface of basic_format<>. Any comments? -Thorsten

Thorsten Ottosen wrote:
Hi,
In code like this:
std::string columns() const { return (boost::format("%7s, %7s, %7s, %7s") % "testCost" % "optimalEcr" % "efficiecyEcr" % "testCostEcr").str(); }
it is very annoying to have to put () around the whole expression and then call .str() (Just imagine that we are creating a string on the fly to a function).
Therefore I suggest that we add
operator string_type() const { return str(); }
to the interface of basic_format<>.
Any comments? You could write it as
return boost::str(boost::format("%7s, %7s, %7s, %7s") % "testCost" % "optimalEcr" % "efficiecyEcr" % "testCostEcr"); Sebastian

Sebastian Redl skrev:
Thorsten Ottosen wrote:
Hi,
In code like this:
std::string columns() const { return (boost::format("%7s, %7s, %7s, %7s") % "testCost" % "optimalEcr" % "efficiecyEcr" % "testCostEcr").str(); }
it is very annoying to have to put () around the whole expression and then call .str() (Just imagine that we are creating a string on the fly to a function).
Therefore I suggest that we add
operator string_type() const { return str(); }
to the interface of basic_format<>.
Any comments? You could write it as
return boost::str(boost::format("%7s, %7s, %7s, %7s") % "testCost" % "optimalEcr" % "efficiecyEcr" % "testCostEcr");
yes, which is hardly any better than the above. Why is such a function even there? I my mind formatting is so closely tied to strings that I don't see any problem in the implicit conversion. IIRC, String.Format in other langauages return a String. -Thorsten

Therefore I suggest that we add
operator string_type() const { return str(); }
to the interface of basic_format<>.
Any comments?
Sorry to dig this up, but I am +10 for this change. I'm currently in the process of removing hundreds of old fixed buffer + sprintf() style log messages from our code and this would make my life a *lot* easier (not to mention more readable). The previous discussion didn't go too far, but if there is general support for this change is there any reason it could not be included? Regards, Alex MDC

Alex MDC wrote:
Therefore I suggest that we add
operator string_type() const { return str(); }
to the interface of basic_format<>.
Any comments?
Sorry to dig this up, but I am +10 for this change.
I'm currently in the process of removing hundreds of old fixed buffer + sprintf() style log messages from our code and this would make my life a *lot* easier (not to mention more readable).
Hi Alex, Why do you prefer boost.format to sprintf? I gave up on boost.format when I realised that, like std::operator<< and boost::lexical_cast, it formats a uint8_t as a char even when I use %d. I now use something like this to get a std::string using sprintf syntax: #include <string> #include <cstdarg> #include <cstdio> #include <malloc.h> std::string format(const char* fmt,...) __attribute__ ((format (printf, 1, 2))); std::string format(const char* fmt,...) { va_list args; va_start(args,fmt); char* p; int r = vasprintf(&p,fmt,args); if (r<0) { throw "vasprintf failed"; } va_end(args); std::string s(p); free(p); return s; } Of course this lacks the ability to output types that define their own operator<<; the only one that I really miss is std::string. Maybe this can be fixed when we have variadic templates. Phil.

Phil Endecott schrieb:
Alex MDC wrote:
Therefore I suggest that we add
operator string_type() const { return str(); }
to the interface of basic_format<>.
Any comments?
Sorry to dig this up, but I am +10 for this change.
I'm currently in the process of removing hundreds of old fixed buffer + sprintf() style log messages from our code and this would make my life a *lot* easier (not to mention more readable).
Hi Alex,
Why do you prefer boost.format to sprintf? I gave up on boost.format when I realised that, like std::operator<< and boost::lexical_cast, it formats a uint8_t as a char even when I use %d. I now use something like this to get a std::string using sprintf syntax:
for me also +10 for the change. maybe it should be considered to make work also %d for a uint8_t... Hansjörg
participants (7)
-
Alex MDC
-
Andrey Semashev
-
Hansi
-
Max
-
Phil Endecott
-
Sebastian Redl
-
Thorsten Ottosen