fork download
  1. #include <iostream>
  2.  
  3. struct low_priority {};
  4. struct high_priority : low_priority {};
  5. struct highest_priority : high_priority {};
  6.  
  7. template<typename T>
  8. class Mixin
  9. {
  10. private:
  11.  
  12. template<typename T2>
  13. static auto CallImpl( highest_priority const& ) -> decltype( T2::SomeFunc(), void() )
  14. {
  15. std::cout << "1\n";
  16. T::SomeFunc();
  17. }
  18.  
  19. template<typename T2>
  20. static auto CallImpl( high_priority const& ) -> decltype( T2{}.SomeFunc(), void() )
  21. {
  22. std::cout << "2\n";
  23. T{}.SomeFunc();
  24. }
  25.  
  26. template<typename T2>
  27. void CallImpl( low_priority const& )
  28. {
  29. std::cout << "3\n";
  30. DefaultImpl();
  31. }
  32.  
  33. public:
  34.  
  35. void Call()
  36. {
  37. CallImpl<T>(highest_priority{});
  38. }
  39.  
  40. void DefaultImpl()
  41. {
  42. std::cout << "Mixin::DefaultImpl\n";
  43. }
  44. };
  45.  
  46. struct A
  47. {
  48. static void SomeFunc()
  49. {
  50. std::cout << "A::SomeFunc()\n";
  51. }
  52. };
  53.  
  54. struct C
  55. {
  56. void SomeFunc()
  57. {
  58. std::cout << "C::SomeFunc()\n";
  59. }
  60. };
  61.  
  62. struct B {};
  63.  
  64. int main()
  65. {
  66. Mixin<A>{}.Call();
  67. Mixin<B>{}.Call();
  68. Mixin<C>{}.Call();
  69. }
  70.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1
A::SomeFunc()
3
Mixin::DefaultImpl
2
C::SomeFunc()