Skip to content

Instantly share code, notes, and snippets.

@alepez
Created July 25, 2014 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alepez/f837571c9dcf50ff4541 to your computer and use it in GitHub Desktop.
Save alepez/f837571c9dcf50ff4541 to your computer and use it in GitHub Desktop.
Create a string as printf do
#ifndef FORMAT_H_
#define FORMAT_H_
#include <string>
#include <cstdarg>
#include <cstdio>
class Format: public std::string {
public:
inline Format(const char* format, ...) {
va_list args;
va_start(args, format);
size_t const bufferSize = ::vsnprintf(0, 0, format, args) + 1;
this->resize(bufferSize);
va_end(args);
va_start(args, format);
::vsnprintf(&((*this)[0]), bufferSize, format, args);
va_end(args);
}
};
#endif /*FORMAT_H_*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment