fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Object
  5. {
  6. public:
  7. void Test()
  8. {
  9. std::cout << "Test" << std::endl;
  10. }
  11. };
  12.  
  13. class EmptyObject
  14. {
  15. };
  16.  
  17. class Base
  18. {
  19. public:
  20. virtual std::string Foo()
  21. {
  22. return "Base";
  23. }
  24. };
  25.  
  26. template <typename T>
  27. class Derived : public Base
  28. {
  29. public:
  30. virtual std::string Foo() override
  31. {
  32. return doFoo(std::is_same<T, Object>());
  33. }
  34.  
  35. private:
  36. std::string doFoo(std::true_type)
  37. {
  38. m_object.Test();
  39. return "Derived";
  40. }
  41. std::string doFoo(std::false_type)
  42. {
  43. return Base::Foo();
  44. }
  45. T m_object;
  46. };
  47.  
  48. int main()
  49. {
  50. Derived<Object> testObject;
  51. std::cout << testObject.Foo() << std::endl;
  52.  
  53. Derived<EmptyObject> testEmpty;
  54. std::cout << testEmpty.Foo() << std::endl;
  55. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Test
Derived
Base