Skip to content

Instantly share code, notes, and snippets.

@jeremy-rifkin
Last active September 14, 2024 21:23
Show Gist options
  • Save jeremy-rifkin/f7dc7c0e46f0034c84e97081cce4a4b7 to your computer and use it in GitHub Desktop.
Save jeremy-rifkin/f7dc7c0e46f0034c84e97081cce4a4b7 to your computer and use it in GitHub Desktop.
c++ string formatting
#include <assert.h>
#include <string>
template<typename... T> std::string stringf(T... args) {
int length = snprintf(0, 0, args...);
if(length < 0) assert(("invalid arguments to stringf", false));
std::string str(length, 0);
snprintf(str.data(), length + 1, args...);
return str;
}
/* Example usage:
* std::string str = stringf("foo %d", 42);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment