fork download
  1. #include <iostream>
  2.  
  3. class A {};
  4. class B {};
  5. class A1 {};
  6. class B1 {};
  7.  
  8. template<class A, class B, class extra = void /* Enable/disable specialization through SFINAE */>
  9. class Class { public: void hello() {std::cout << "Base" << std::endl;} };
  10.  
  11. template<class B> class Class<A1, B> { public: void hello() {std::cout << "First" << std::endl;} };
  12. template<class A> class Class<A, B1, typename std::enable_if<!std::is_same<A, A1>::value>::type> { public: void hello() {std::cout << "Second" << std::endl;} };
  13.  
  14. int main()
  15. {
  16.  
  17. Class<A1,B1> obj;// The first one is picked up
  18. obj.hello();
  19. Class<A,B1> obj2; // The second one is picked up
  20. obj2.hello();
  21. Class<A,B> obj3; // Base
  22. obj3.hello();
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
First
Second
Base