Created
May 16, 2016 07:59
-
-
Save safiire/92606e854f76c224b6ebc8b83eaf4585 to your computer and use it in GitHub Desktop.
Interesting way of using the new variadic templates to create a variadic print() function.
This file contains hidden or 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
| // Compile with: g++ -std=c++11 variadic_print.cpp -o variadic_print | |
| #include <iostream> | |
| // No argument case | |
| void print() {} | |
| // Recursive Variadic Template | |
| template <typename HEAD, typename ... TAIL> | |
| void print(const HEAD& head, const TAIL& ... tail) { | |
| std::cout << head << std::endl; | |
| print(tail...); | |
| } | |
| int main(){ | |
| print(1, 2, 3, 3.14159, "Slick use of templates"); | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fun, but for each call with
narguments it actually generatesnseparate but nearly identical templated print<> functions, even at -O3.ie
Not practical, but I still thought it was cute.