#include <cstdio>
#include <cstdarg>
#include <functional>
class PrintWrapper
{
public:
PrintWrapper() = default;
template<typename T>
PrintWrapper( T&& t) : func(std::forward<T>(t))
{
}
int operator()(char const* format, ...)
{
va_list args;
va_start(args, format);
int result = func(format, args);
va_end(args);
return result;
}
private:
std::function< int(char const*, va_list)> func;
};
int main()
{
// Note, you have to use the 'v' versions
PrintWrapper p = std::vprintf;
p("%d %d %s\n", 1,2, "hello");
char buffer[256];
using namespace std::placeholders;
p = std::bind(std::vsnprintf, buffer, sizeof(buffer), _1, _2 );
p("%lf %s\n", 0.1234, "goodbye");
// Since the previous step was a wrapper around snprintf, we need to print
// the buffer it wrote into
printf("%s\n", buffer);
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRhcmc+CiNpbmNsdWRlIDxmdW5jdGlvbmFsPgoKY2xhc3MgUHJpbnRXcmFwcGVyCnsKICAgIAogICAgcHVibGljOgogICAgUHJpbnRXcmFwcGVyKCkgID0gZGVmYXVsdDsKCiAgICAKICAgIHRlbXBsYXRlPHR5cGVuYW1lIFQ+CiAgICBQcmludFdyYXBwZXIoIFQmJiB0KSAgOiBmdW5jKHN0ZDo6Zm9yd2FyZDxUPih0KSkKICAgIHsKICAgICAgICAKICAgIH0KICAgIAogICAgCiAgICBpbnQgb3BlcmF0b3IoKShjaGFyIGNvbnN0KiBmb3JtYXQsIC4uLikKICAgIHsKICAgICAgICB2YV9saXN0IGFyZ3M7CiAgICAgICAgdmFfc3RhcnQoYXJncywgZm9ybWF0KTsKICAgICAgICBpbnQgcmVzdWx0ID0gZnVuYyhmb3JtYXQsIGFyZ3MpOwogICAgICAgIHZhX2VuZChhcmdzKTsKICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgfQogICAgcHJpdmF0ZToKICAgIAogICAgc3RkOjpmdW5jdGlvbjwgaW50KGNoYXIgY29uc3QqLCB2YV9saXN0KT4gZnVuYzsKICAgIAp9OwoKaW50IG1haW4oKQp7CiAgICAv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