fork download
  1. #include <cstddef>
  2. #include <iostream>
  3.  
  4. template <typename T> struct Printable
  5. {
  6. Printable(const T& val) : val(val) {}
  7. void print(std::ostream& out) const {out << val;}
  8. const T& val;
  9. };
  10.  
  11. template <> struct Printable<std::nullptr_t>
  12. {
  13. Printable(nullptr_t) {}
  14. void print(std::ostream& out) const {out << "null";}
  15. };
  16.  
  17. template <typename T>
  18. Printable<T> printable(const T& value) {return Printable<T>(value);}
  19.  
  20. template <typename T>
  21. std::ostream& operator<<(std::ostream& out, const Printable<T>& p)
  22. {
  23. p.print(out);
  24. return out;
  25. }
  26.  
  27. int main()
  28. {
  29. std::cout << printable(42) << " " << printable(nullptr) << "\n";
  30. return 0;
  31. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
42 null