#include <string>
#include <memory>
#include <cstring>
#include <cstdarg>
#include <cstdio>

static std::string string_format(const std::string fmt_str, ...)
{
    int final_n, n = ((int)fmt_str.size()) * 2; /* Reserve two times as much as the length of the fmt_str */
    std::unique_ptr<char[]> formatted;
    va_list ap;
    while(1) {
        formatted.reset(new char[n]); /* Wrap the plain char array into the unique_ptr */
        std::strcpy(&formatted[0], fmt_str.c_str());
        va_start(ap, fmt_str);
        final_n = vsnprintf(&formatted[0], n, fmt_str.c_str(), ap);
        va_end(ap);
        if (final_n < 0 || final_n >= n)
            n += std::abs(final_n - n + 1);
        else
            break;
    }
    return std::string(formatted.get());
}

int main()
{
	puts(string_format("%s %d", "test", 1).c_str());
}