fork download
  1. #include <iostream>
  2.  
  3. using E = int;
  4. using r = int;
  5.  
  6. template<typename TYPE>
  7. class Target5
  8. {
  9. public:
  10. Target5(const TYPE &value) : value(value) {}
  11. TYPE value;
  12.  
  13. template <typename T>
  14. void OneParam(T a)
  15. {
  16. std::cout << "Target5::OneParam(" << value << "," << a << ")\n";
  17.  
  18. typedef void (Target5<E>::*MethodTypeToCall)(T);
  19. // Here, the compiler picks the right overload
  20. MethodTypeToCall toCall = &Target5<E>::Private<T>;
  21. // In this case, the compiler does not let us write the following line (parse error):
  22. //MethodTypeToCall toCall = &Target5<E>::Private<t;;
  23. (this->*toCall)(a);
  24. }
  25.  
  26. template <typename T1, typename T2>
  27. void TwoParam(T1 a, T2 b)
  28. {
  29. std::cout << "Target5::TwoParam(" << value << "," << a << "," << b << ")\n";
  30.  
  31. typedef void (Target5<E>::*MethodTypeToCall)(T1, T2);
  32. MethodTypeToCall toCall = &Target5<E>::Private<T1, T2>; // compiler picks the right overload
  33. // you can't add the method's template parameters to the end of that line
  34. (this->*toCall)(a, b);
  35. }
  36.  
  37. private:
  38.  
  39. template <typename T>
  40. void Private(T a)
  41. {
  42. std::cout << "Target5::Private(" << value << "," << a << ")\n";
  43. }
  44. template <typename T1, typename T2>
  45. void Private(T1 a, T2 b)
  46. {
  47. std::cout << "Target5::Private(" << value << "," << a << "," << b << ")\n";
  48. }
  49. };
  50.  
  51. inline
  52. void HoldingAPointerToTemplateMemberInTemplateClass()
  53. {
  54. Target5<r> target('c');
  55.  
  56. void (Target5<r>::*oneParam)(int) = &Target5<r>::OneParam;
  57. (target.*oneParam)(7);
  58. void (Target5<r>::*twoParam)(float, int) = &Target5<r>::TwoParam;
  59. (target.*twoParam)(7.5, 7);
  60. }
  61.  
  62. int main()
  63. {
  64. HoldingAPointerToTemplateMemberInTemplateClass();
  65. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
Target5::OneParam(99,7)
Target5::Private(99,7)
Target5::TwoParam(99,7.5,7)
Target5::Private(99,7.5,7)