fork download
  1. #include <iostream>
  2.  
  3. template <int K>
  4. struct postbox
  5. {
  6. int val() const
  7. {
  8. return K;
  9. }
  10. };
  11.  
  12. template <int A, int B, int C>
  13. struct postoffice
  14. {
  15. postbox<A> _a;
  16.  
  17. template<int I>
  18. postbox<I> get_postbox()
  19. {
  20. switch( I )
  21. {
  22. case A: return _a;
  23. }
  24. }
  25. };
  26.  
  27. template <typename PO>
  28. struct zipcode
  29. {
  30. PO my_postoffice;
  31.  
  32. template<int K>
  33. postbox<K> get_postbox()
  34. {
  35. // The error is on this line
  36. return my_postoffice.template get_postbox<K>();
  37. }
  38. };
  39.  
  40. // Here's a function template that isn't a member, and it compiles.
  41. template<int D>
  42. int non_member_function_template()
  43. {
  44. postoffice<123,345,678> po;
  45. auto box = po.template get_postbox<D>();
  46. return box.val();
  47. }
  48.  
  49. int main()
  50. {
  51. std::cout << std::to_string(non_member_function_template<123>()) << std::endl;
  52. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
123