Created
March 15, 2015 14:18
-
-
Save emilk/dfc1071014f7b81917ff to your computer and use it in GitHub Desktop.
strprintf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::string strprintf(const char * __restrict, va_list) __printflike(1, 0); | |
std::string strprintf(const char * __restrict, ...) __printflike(1, 2); | |
std::string strprintf(const char* format, va_list vlist) | |
{ | |
#if 1 | |
char* buff = nullptr; | |
int ret = vasprintf(&buff, format, vlist); | |
ASSERT(ret >= 0); | |
std::string str = buff; | |
free(buff); | |
return str; | |
#else | |
int bytes_needed = vsnprintf(nullptr, 0, format, vlist); | |
ASSERT(bytes_needed >= 0); | |
//std::string str; // str.data() is const char* (grrr) | |
std::vector<char> str; | |
str.resize(bytes_needed + 1); | |
vsnprintf(str.data(), str.size(), format, vlist); | |
return std::string(std::begin(str), std::end(str)); // TODO: avoid this copy | |
#endif | |
} | |
std::string strprintf(const char* format, ...) | |
{ | |
va_list vlist; | |
va_start(vlist, format); | |
auto str = strprintf(format, vlist); | |
va_end(vlist); | |
return str; | |
} |
std::string strprintf(const char* format, va_list vlist)
problem is for va_list we don't even know if it has a copy constructor
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can I propose the following implementation instead?