gpt4 book ai didi

c++ - 如何调用模板成员函数?

转载 作者:IT老高 更新时间:2023-10-28 12:49:51 25 4
gpt4 key购买 nike

Possible Duplicate:
C++ template member function of template class called from template function

template<class T1>
class A
{
public:
template<class T0>
void foo() const {}
};

template<class T0,class T1>
void bar( const A<T1>& b )
{
b.foo<T0>(); // This throws " expected primary-expression before ‘>’ token"
}

我可以改成

b->A<T1>::template foo<T0>();

编译得很好。不过我也可以改成

b.A<T1>::template foo<T0>();

编译也很好。嗯?

如何正确调用原始代码意义上的模板成员函数?

最佳答案

刚刚找到:

根据 C++'03 标准 14.2/4:

When the name of a member template specialization appears after . or -> in a postfix-expression, or after nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a template-parameter (14.6.2), the member template name must be prefixed by the keyword template. Otherwise the name is assumed to name a non-template.

正确的代码是:

b.template foo<T0>();

关于c++ - 如何调用模板成员函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12676190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com