fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. namespace cpp {
  5.  
  6. void printf(const char* s)
  7. {
  8. while (*s) {
  9. if (*s == '%' && *++s != '%')
  10. throw std::runtime_error("invalid format string: missing arguments");
  11. std::cout << *s++;
  12. }
  13. }
  14.  
  15. template<typename T, typename... Args>
  16. void printf(const char* s, const T& value, const Args&... args)
  17. {
  18. while (*s) {
  19. if (*s == '%' && *++s != '%') {
  20. std::cout << value;
  21. return printf(++s, args...);
  22. }
  23. std::cout << *s++;
  24. }
  25. throw std::runtime_error("extra arguments provided to printf");
  26. }
  27.  
  28. } // namespace cpp
  29.  
  30. int main()
  31. {
  32. // classic printf
  33. printf("%u\n", -1);
  34.  
  35. // C++ variadic printf
  36. cpp::printf("%u\n", -1);
  37. }
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
4294967295
-1