calling template function of template base class [duplicate]

隐身守侯 提交于 2019-11-28 23:25:58
Mike Seymour

foo is a dependent name, so the first-phase lookup assumes that it's a variable unless you use the typename or template keywords to specify otherwise. In this case, you want:

this->template foo<int>();

See this question if you want all the gory details.

You should do it like this :

template<typename T>
class derived : public base<T>
{
public:
    void bar()
    {
        base<T>::template foo<int>();
    } 
};

Here is full compilable example :

#include <iostream>

template<typename T>
class base
{
public:
    virtual ~base(){}

    template<typename F>
    void foo()
    {
        std::cout << "base::foo<F>()" << std::endl;
    }
};

template<typename T>
class derived : public base<T>
{
public:

    void bar()
    {
        base<T>::template foo<int>(); // Compile error
    }
};

int main()
{
  derived< int > a;
  a.bar();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!