Skip to content

Instantly share code, notes, and snippets.

@yalue
Created October 6, 2016 15:50
Show Gist options
  • Save yalue/2c7cdb2ed10e0dceea1908d23f908753 to your computer and use it in GitHub Desktop.
Save yalue/2c7cdb2ed10e0dceea1908d23f908753 to your computer and use it in GitHub Desktop.
How to make using C++ suck less
void StreamPrintf(std::ostream stream, const char *format, ...) {
char buffer[1024];
va_list args;
memset(buffer, 0, sizeof buffer);
va_start(args, format);
vsnprintf(buffer, (sizeof buffer) - 1, format, args);
va_end(args);
stream << buffer;
}
@yamirui
Copy link

yamirui commented Jul 5, 2022

Good job on making C++ worse than C in just 9 lines of code.

@yalue
Copy link
Author

yalue commented Jul 5, 2022

lol! Making C++ worse than C doesn't require any code at all!

(I think I wrote this way back when I was needing to print a bunch of lines containing both base-10 and hex values. "0x%08x: %d", a, b was, and always will be, far more convenient than ... << "0x" << std::setfill('0') << std::setw(8) << std::hex << a << std::dec << " " << b)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment