Last active
September 14, 2024 21:23
-
-
Save jeremy-rifkin/f7dc7c0e46f0034c84e97081cce4a4b7 to your computer and use it in GitHub Desktop.
c++ string formatting
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
#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