---
title: 成员模板函数调用需要添加template前缀
description: "```"
url: https://www.hikunpeng.com/document/detail/zh/kunpengdevps/compilation/ug-bisheng/kunpengbisheng_06_0093.html
sourcePath: /source/zh/kunpengdevps/compilation/ug-bisheng/kunpengbisheng_06_0093.html
indexId: 1ae9dd469c470bb5b9036369470a2c43aeb40335f8b5da075ad5a021d9288a2274
---
# 成员模板函数调用需要添加template前缀

#### 错误信息

```
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;
  }
};
```

```
error: use 'template' keyword to treat 'As' as a dependent template name
   12 |     guest2 ? guest2->As<T>() : nullptr;
      |                      ^
      |                      template
```


#### 问题介绍

C++规范明确要求：

```
ISO C++03 14.2/4：当成员模板特化的名称出现在后缀表达式中的 `.` 或 `->` 之后，或者出现在限定标识符中的嵌套名称说明符之后，并且该后缀表达式或限定标识符明确依赖于模板参数时，成员模板名称必须以关键字 `template` 作为前缀。否则，该名称将被假定为一个非模板。
```


#### 解决方案

- 添加template前缀；
- 使用clang-tidy工具可以识别该类问题并提供修改建议。
