fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. template <typename, typename>
  6. struct foo_Impl;
  7.  
  8. template <typename T>
  9. class A
  10. {
  11. public:
  12. template <typename U>
  13. void foo() const { foo_Impl<T, U>(*this); }
  14.  
  15. private:
  16. template <typename, typename>
  17. friend struct foo_Impl;
  18.  
  19. void print(const std::string& text) const
  20. {
  21. std::cout << this << ": " << text <<std::endl;
  22. }
  23. };
  24.  
  25. template <typename T, typename>
  26. struct foo_Impl
  27. {
  28. foo_Impl(const A<T>& owner) { owner.print("general implementation"); }
  29.  
  30. };
  31.  
  32. template <typename T>
  33. struct foo_Impl<T, double>
  34. {
  35. foo_Impl(const A<T>& owner) { owner.print("specialization for double"); }
  36. };
  37.  
  38. template <typename T, typename U>
  39. struct foo_Impl<T, std::vector<U>>
  40. {
  41. foo_Impl(const A<T>& owner) { owner.print("specialization for vectors of all types"); }
  42. };
  43.  
  44. int main() {
  45.  
  46. A<int> a;
  47.  
  48. a.foo<int>();
  49. a.foo<double>();
  50. a.foo<std::vector<bool>>();
  51. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
0x7ffc890570ef: general implementation
0x7ffc890570ef: specialization for double
0x7ffc890570ef: specialization for vectors of all types