Skip to content

Instantly share code, notes, and snippets.

@qwertyowmx
Created January 18, 2023 14:54
Show Gist options
  • Save qwertyowmx/d083405d31eda6261c746e878dc962fa to your computer and use it in GitHub Desktop.
Save qwertyowmx/d083405d31eda6261c746e878dc962fa to your computer and use it in GitHub Desktop.
C++ variadic println
#include <iostream>
#include <string>
namespace out {
struct OutConf {
std::string sep = " ";
std::string end = "\n";
std::ostream& file = std::cout;
bool flush = false;
};
template <typename ... Args>
void print(OutConf outConfiguration = {}, Args ... args) {
((outConfiguration.file << args << outConfiguration.sep), ...);
outConfiguration.file << outConfiguration.end;
if (outConfiguration.flush) {
outConfiguration.file << std::flush;
}
}
template <typename ... Args>
void println(OutConf outConfiguration = {}, Args ... args) {
((outConfiguration.file << args << outConfiguration.sep), ...);
outConfiguration.file << outConfiguration.end;
if (outConfiguration.flush) {
outConfiguration.file << std::endl;
}
}
}
int main()
{
out::print({.sep = "abcde", .end="ooo\n", .flush=true}, 1, 2, 3, 4, 5);
out::println({ .sep = "_" }, "abc", 2, 3, 1, 5, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment