fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <string_view>
  4.  
  5. constexpr bool validate(std::string_view view, int n)
  6. {
  7. int i = 0;
  8. for (auto& c : view)
  9. {
  10. if (c == '%')
  11. {
  12. i++;
  13. }
  14. }
  15. return i == n;
  16. }
  17.  
  18. template<class StrLiteral, class ...Vars>
  19. void fmt(StrLiteral str, const Vars&... args)
  20. {
  21. constexpr std::string_view view = str();
  22.  
  23. static_assert(validate(view, sizeof...(args)), "Wrong format");
  24.  
  25. printf(view.data(), args...);
  26. }
  27.  
  28. template<class ...Vars>
  29. void fmt(std::string str, const Vars& ... args)
  30. {
  31. printf(str.c_str(), args...);
  32. }
  33.  
  34. #define _F(str) []() { return str; }
  35.  
  36. void main()
  37. {
  38. fmt(_F("compile %d\n"), 555);
  39. fmt(std::string("runtime no check %d\n"), 555);
  40. fmt(_F("compile no args\n"));
  41. //fmt(_F("compile error\n"), 555);
  42. return;
  43. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:23: fatal error: string_view: No such file or directory
 #include <string_view>
                       ^
compilation terminated.
stdout
Standard output is empty