fork download
  1. #include <iostream>
  2.  
  3. void typesafe_printf(const char* format) // Rekursionsende des Templates
  4. {
  5. while (*format) {
  6. if (*format == '%') {
  7. throw std::runtime_error("Mehr Platzhalter als Parameter");
  8. }
  9. std::cout << *format++;
  10. }
  11. };
  12.  
  13. template<typename T, typename... Rest>
  14. void typesafe_printf(const char* format, T param, Rest... params)
  15. {
  16. while (*format) {
  17. if (*format == '%') {
  18. std::cout << param;
  19. format++;
  20. typesafe_printf(format, params...);
  21. return;
  22. }
  23. std::cout << *format++;
  24. }
  25. };
  26.  
  27.  
  28. #include <string>
  29. #include <complex>
  30.  
  31. int main()
  32. {
  33. typesafe_printf("Ein char: %, viele chars: %, ein string: %, ein int: %, ein double: %, ein complex: %",
  34. 'a', "abc", std::string("blah"), 123, 123.123, std::complex<double>(20.2,2.5)
  35. );
  36. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Ein char: a, viele chars: abc, ein string: blah, ein int: 123, ein double: 123.123, ein complex: (20.2,2.5)