J
Jorgen Grahn
I should know this, but ...
Is this code safe?
std::string foo();
std:rintf("%s", foo().c_str());
Or is my temporary std::string destroyed before printf()
is done with it?
Background:
I'm sitting with a whole bunch of classes and (for now) only a
printf-like interface for printing logs/debug info. I was thinking
about
- implementing ostream << foo as usual,
- have a 'template<T> std:string to_string(const T&)' based on it
- log using the slightly ugly syntax
log("error in %s: %s",
to_string(foo).c_str(),
to_string(bar).c_str());
It's ugly and extremely wasteful, but I want to define the output
format in *one* place for each class -- not in every place where I might
want to print it.
/Jorgen
Is this code safe?
std::string foo();
std:rintf("%s", foo().c_str());
Or is my temporary std::string destroyed before printf()
is done with it?
Background:
I'm sitting with a whole bunch of classes and (for now) only a
printf-like interface for printing logs/debug info. I was thinking
about
- implementing ostream << foo as usual,
- have a 'template<T> std:string to_string(const T&)' based on it
- log using the slightly ugly syntax
log("error in %s: %s",
to_string(foo).c_str(),
to_string(bar).c_str());
It's ugly and extremely wasteful, but I want to define the output
format in *one* place for each class -- not in every place where I might
want to print it.
/Jorgen