Rate This Document
Findability
Accuracy
Completeness
Readability

The Template Prefix Needs to Be Added for Calling Member Template Functions

Error Information

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class A {
public:
  template <typename> void As();
  static A *FromWebContents();
  A *FromWebContents2();
};
template <typename T> class B : A {
  void FromWebContents() {
    auto guest = A::FromWebContents();
    guest ? guest->As<T>() : nullptr;
    auto guest2 = A::FromWebContents2();
    guest2 ? guest2->As<T>() : nullptr;
  }
};
1
2
3
4
error: use 'template' keyword to treat 'As' as a dependent template name
   12 |     guest2 ? guest2->As<T>() : nullptr;
      |                      ^
      |                      template

Problem

The C++ specifications clearly require:

1
ISO C++03 14.2/4: When the specialized name of a member template appears after `.` or `->` in a postfix expression or after a nested-name-specifier in a qualified identifier, and the postfix expression or qualified identifier explicitly depends on a template parameter, the member template name must be prefixed by the keyword `template`. Otherwise, the name is assumed to be a non-template.

Solution

  • Add the template prefix.
  • Use the clang-tidy tool to identify such issues and provide modification suggestions.