Boost logo

Boost :

Subject: Re: [boost] [format] please add operator String()
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2009-02-06 06:43:51


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.


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