fork download
  1.  
  2. #include <iostream>
  3.  
  4. class A
  5. {
  6. public:
  7. void foo()
  8. {
  9. std::cout << __func__ << " calling\n";
  10. }
  11. void special_bar_A()
  12. {
  13. std::cout << __func__ << " calling\n";
  14. }
  15. void baz()
  16. {
  17. std::cout << __func__ << " calling\n";
  18. }
  19. };
  20.  
  21. class B
  22. {
  23. public:
  24. void foo()
  25. {
  26. std::cout << __func__ << " calling\n";
  27. }
  28. void special_bar_B()
  29. {
  30. std::cout << __func__ << " calling\n";
  31. }
  32. void baz()
  33. {
  34. std::cout << __func__ << " calling\n";
  35. }
  36. };
  37.  
  38. template<class T> void special_stuff(T &)
  39. {
  40. std::cout << "something went wrong\n";
  41. }
  42.  
  43. template<>
  44. void special_stuff(A &a)
  45. {
  46. a.special_bar_A();
  47. }
  48.  
  49. template<>
  50. void special_stuff(B &b)
  51. {
  52. b.special_bar_B();
  53. }
  54.  
  55. template<class T>
  56. void do_stuff()
  57. {
  58. T t;
  59. t.foo();
  60. special_stuff<T>(t);
  61. t.baz();
  62. }
  63.  
  64. int main()
  65. {
  66. do_stuff<A>();
  67. do_stuff<B>();
  68. }
  69.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
foo calling
special_bar_A calling
baz calling
foo calling
special_bar_B calling
baz calling