Created
January 18, 2023 14:54
-
-
Save qwertyowmx/d083405d31eda6261c746e878dc962fa to your computer and use it in GitHub Desktop.
C++ variadic println
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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