fork download
  1. #include <iostream>
  2.  
  3. template <typename T = void>
  4. class Class {
  5. public:
  6. template <
  7. typename U,
  8. typename = typename std::enable_if<
  9. // Is the use of T here allowed?
  10. std::is_void<T>::value
  11. || std::is_base_of<T, U>::value
  12. >::type
  13. >
  14. Class(U &&arg) {
  15. std::cout << "Derived" << std::endl;
  16. }
  17.  
  18. template <
  19. typename U,
  20. typename ...U_Rest,
  21. typename = typename std::enable_if<
  22. // Is the use of T here allowed?
  23. !std::is_void<T>::value
  24. && !std::is_base_of<T, U>::value
  25. >::type
  26. >
  27. Class(U &&arg, U_Rest &&...rest) {
  28. std::cout << "Not Derived" << std::endl;
  29. }
  30. };
  31.  
  32. class Base { };
  33. class Derived : public Base { };
  34.  
  35. int main() {
  36. // Initialize with derived.
  37. Class<Base> c0 { Derived() };
  38. // Initialize with not derived.
  39. Class<Derived> c1 { Base() };
  40. // Use same constructor as if derived.
  41. Class<> c2 { Derived() };
  42. }
  43.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Derived
Not Derived
Derived