Created
September 15, 2023 05:38
-
-
Save ultimatile/a2012e737218683755d1a68aaa1824a7 to your computer and use it in GitHub Desktop.
println function for C++>=17
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
/* | |
a print function with line break for standard output. | |
usage: println(args...) | |
args are variadic arguments to display. | |
It behaves in the same way as the following standard output format; | |
std::cout << args[0] << args[1] << ... << std::endl; | |
This fuction needs C++17. | |
*/ | |
#include <iostream> | |
#include <string> | |
template<typename... Args> | |
void print_all(std::ostream& os, Args... args) | |
{ | |
(os << ... << args); | |
} | |
template<typename... Args> | |
void println(Args... args) | |
{ | |
print_all(std::cout, args...); | |
std::cout << std::endl; | |
} | |
// example | |
int main() { | |
int x = 42; | |
double y = 3.14; | |
std::string text = "Hello, World!"; | |
println(text); | |
println("x= ", x, " y= ", y); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment