Skip to content

Instantly share code, notes, and snippets.

@AlexBolotsin
Created August 25, 2017 17:54
Show Gist options
  • Save AlexBolotsin/ed07011c9bead4e1844cdcf1a848adbb to your computer and use it in GitHub Desktop.
Save AlexBolotsin/ed07011c9bead4e1844cdcf1a848adbb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std::string_literals;
void tprintf(const char* format) // base function
{
std::cout << format;
}
template<typename T, typename... Targs>
void tprintf(const char* format, T value, Targs... Fargs) // recursive variadic function
{
for ( ; *format != '\0'; format++ ) {
if ( *format == '%' ) {
std::cout << value;
tprintf(format+1, Fargs...); // recursive call
return;
}
std::cout << *format;
}
}
template <typename T>
auto add(T value) // base function
{
return value;
}
template<typename T, typename... Targs>
auto add(T value, Targs... Fargs) // recursive variadic function
{
return value + add(Fargs...);
}
int main()
{
tprintf("% world% %\n","Hello",'!',123);
int val = add(1, 3, 12);
std::cout << val << "\n";
auto str_val = add("the"s, ' ', "song"s, ' ', "is"s, ' ', "beautiful"s);
std::cout << str_val << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment