I don't see a constructor for std::string
that can consume a va_list
. Is there a common solution for converting a va_list
to a std::string
?
I've seen solutions in the form of:
std::string vstring (const char * format, ...) {
std::string result;
va_list args;
va_start(args, format);
char buffer[1024];
vsnprintf(buffer, sizeof(buffer), format, args);
result = std::string(buffer);
va_end(args);
return result;
}
This feels error prone and hacky. Is there a way for std::string
to be constructed from or operate on a va_list
directly?
NOTE: The main problem I have with the solution above is the need to guess at the amount of memory I need. I don't want to waste too much or not have enough. Ideally, I would like a
std::string
style opaque allocation that just works.NOTE: I need a solution that does not require third party library support.