调用模板基类的模板函数 [英] calling template function of template base class

查看:111
本文介绍了调用模板基类的模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

以下是代码:

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()
    {
        this->foo<int>(); // Compile error
    } 
};

运行时:

derived<bool> d;
d.bar();

我收到以下错误:

error: expected primary-expression before ‘int’
error: expected ‘;’ before ‘int’

我知道非依赖名称和2阶段查找。但是,当函数本身是一个模板函数( foo<>()在我的代码中),我尝试所有的解决方法只是失败。

I'm aware of non-dependent names and 2-phase look-ups. But, when the function itself is a template function (foo<>() function in my code), I tried all workarounds only to fail.

推荐答案

foo 是一个依赖名称,因此第一阶段查找假设它是一个变量,除非您可以使用 typename 模板关键字来指定。在这种情况下,您需要:

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.

这篇关于调用模板基类的模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆