Advertisement
ulfben

Basic printf-a-like

Jan 5th, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. namespace utils{
  3.     void printf(const char* format){
  4.         std::cout << format;
  5.     }
  6.     template<typename T, typename ... Args>
  7.     void printf(const char* format, T value, Args ... args){
  8.         for ( ; *format != '\0'; format++ ) {
  9.             if ( *format == '%' ) {
  10.                std::cout << value;
  11.                printf(format + 1, args ... );
  12.                return;
  13.             }
  14.             std::cout << *format;
  15.         }
  16.     }
  17.     template<typename T, typename ... Args>
  18.     void printf(const std::string& format, T value, Args ... args){
  19.         printf(format.c_str(), value, args...);
  20.     }
  21. }
  22.  
  23. int main(){
  24.     utils::printf('% world% %\n', "Hello", '!', 2019);                            
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement