成员模板函数调用需要添加template前缀
错误信息
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 |
问题介绍
C++规范明确要求:
1 | ISO C++03 14.2/4:当成员模板特化的名称出现在后缀表达式中的 `.` 或 `->` 之后,或者出现在限定标识符中的嵌套名称说明符之后,并且该后缀表达式或限定标识符明确依赖于模板参数时,成员模板名称必须以关键字 `template` 作为前缀。否则,该名称将被假定为一个非模板。 |
解决方案
- 添加template前缀;
- 使用clang-tidy工具可以识别该类问题并提供修改建议。
父主题: 其它类兼容问题