fork download
  1. #include <cstdio>
  2. #include <iostream>
  3.  
  4. template<size_t BufSize, typename... Args>
  5. int strprintf(char(&buf)[BufSize], const char* fmt, Args&&... args)
  6. {
  7. static_assert(BufSize > 0, "Buffer too small");
  8. static_assert(BufSize < (1 << 31), "Buffer too large");
  9. return snprintf(buf, BufSize, fmt, std::forward<Args>(args)...);
  10. }
  11.  
  12. int main() {
  13. char buf[16];
  14. int printed = strprintf(buf, "hello world %s so long", "oversized load");
  15. std::cout << buf << "\n";
  16. }
  17.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
hello world ove