fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class c1 {
  6. public:
  7. void f1(){std::cout<<"In f1\n";}
  8. };
  9.  
  10. class c2 {
  11. public:
  12. void f2(){std::cout<<"In f2\n";}
  13. };
  14.  
  15. template<typename> struct which_member;
  16.  
  17. template<> struct which_member<c1> {
  18. static constexpr void (c1::* func)() = &c1::f1;
  19. };
  20.  
  21. template<> struct which_member<c2> {
  22. static constexpr void (c2::* func)() = &c2::f2;
  23. };
  24.  
  25. template <typename T>
  26. class C: public c1, c2 {
  27. public:
  28.  
  29. void f() {
  30. (static_cast<T*>(this)->*which_member<T>::func)();
  31. }
  32. };
  33.  
  34. int main()
  35. {
  36. C<c2> c;
  37. c.f();
  38. return 0;
  39. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
In f2