The most obvious solution is to define a type which contains the
std::string
, and converts implicitly to a char const*
.
Something like:
class ToPlainC
{
std::string myValue
public:
ToPlainC( boost::format const& fmt )
: myValue( fmt.str() )
{
}
operator char const*() const
{
return myValue.c_str();
}
};
which could be used:
lib_function( ToPlainC( boost::format( "%1% %2%" ) % "1" % "2" ) );
Such implicit conversions are not usually a good idea, but if you document the class well, that it should only be used for this particular scenario, I think it would be acceptable.
EDIT:
It occurs to me that to encourage only using this class as a temporary, in this particular scenario, you could name it using the naming conventions you normally use for functions, and not those you use for classes; the user would then have the impression that he was using a function, and it would stick out like a sore thumb if he used it otherwise.