Skip to content

Instantly share code, notes, and snippets.

@byBretema
Last active November 10, 2019 09:53
Show Gist options
  • Save byBretema/9511c5ded05f8ce49599c46d75420d9d to your computer and use it in GitHub Desktop.
Save byBretema/9511c5ded05f8ce49599c46d75420d9d to your computer and use it in GitHub Desktop.
A quick print function that wraps std::cout and allow pass elems of any type to be printed in a function-like way.
#include <iostream>
// ------------------------------------------
// Quick print
// ------------------------------------------
void _printRest(void){}
template <typename H, typename ...T>
void _printRest(const H& head, T&&... tail){ std::cout << " " << head; _printRest(tail...); }
void _printFirst(void){}
template <typename H, typename ...T>
void _printFirst(const H& head, T&&... tail){ std::cout << head; _printRest(tail...); }
auto qprint = [] (auto... param){ _printFirst(param...); };
auto qprintln = [] (auto... param){ _printFirst(param...); std::cout << "\n"; };
// ------------------------------------------
/*
USAGE EXAMPLE:
qprint("My favourite number is:", 42, "\nWhich is yours?");
int n;
std::cin >> n;
qprint("My favourite is:", n);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment