fork(37) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. struct Printable {};
  5. struct Serializable {};
  6. struct DerivedPrintable : Printable {};
  7. struct DerivedSerializable : Serializable {};
  8.  
  9. template <class T, typename std::enable_if<std::is_base_of<Printable, T>::value>::type* = nullptr>
  10. void print(const T& value) {
  11. std::cout << "printable(" << &value << ")\n";
  12. }
  13.  
  14. template <class T, typename std::enable_if<std::is_base_of<Serializable, T>::value>::type* = nullptr>
  15. void print(const T& value) {
  16. std::cout << "serializable(" << &value << ")\n";
  17. }
  18.  
  19. int main() {
  20. print(Printable());
  21. print(DerivedPrintable());
  22. print(Serializable());
  23. print(DerivedSerializable());
  24. }
  25.  
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
printable(0x7fffcec221df)
printable(0x7fffcec221df)
serializable(0x7fffcec221df)
serializable(0x7fffcec221df)