Skip to content

Instantly share code, notes, and snippets.

@saginadir
Created August 13, 2017 07:53
Show Gist options
  • Save saginadir/8feb740041a16fc3afe79473e336f2d8 to your computer and use it in GitHub Desktop.
Save saginadir/8feb740041a16fc3afe79473e336f2d8 to your computer and use it in GitHub Desktop.
Always wanted the JavaScript console.log function in cpp? well now you have it:
void consoleLogHelper() {};
template <typename T>
void print(T t) {
std::cout << t << " ";
}
template <typename T, typename...Ts>
void consoleLogHelper(T &&first, Ts&&... rest) {
// print it
print(std::forward<T>(first));
// Forward the rest.
consoleLogHelper(std::forward<Ts>(rest)...);
}
template <typename...Ts>
void consoleLog(Ts&&... rest) {
consoleLogHelper((rest)...);
std::cout << std::endl;
}
@saginadir
Copy link
Author

Use case:

consoleLog("The boy ate", 3, "Cakes") // -> The boy ate 3 Cakes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment