Created
August 9, 2019 09:52
-
-
Save SLSNe/ed9c43030326c79b4339a143971be3f1 to your computer and use it in GitHub Desktop.
C++17 Python like?? Print Function
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
#pragma once | |
#include <iostream> | |
template<auto sep = ' ', auto end = '\n', typename FirstType, typename... Types> | |
void print(const FirstType& firstArg, const Types... args) | |
{ | |
std::cout << firstArg; | |
auto separator = [](const auto &arg) { | |
std::cout << sep << arg; | |
}; | |
(... , separator(args)); | |
std::cout << end; | |
} | |
/* | |
Example Usage: | |
print(2, 5, 7, 8); Output: 2 5 7 8 | |
print<'-'>(2, 5, 7, 8); Output: 2-5-7-8 | |
print<'-', '\t'>(2, 5, 7, 8) Output: 2-5-7-8\t | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment