相关疑难解决方法(0)

是否隐式实例化了模板类的虚函数?

请考虑以下代码.是否保证Derived<int>::foo()会被实例化?foo()是虚拟的,由基类的非虚函数调用.

#include <iostream>

class Base
{
public:
    void bar() { foo(); }
private:
    virtual void foo() = 0;
};

template <typename T> class Derived: public Base
{
public:
    Derived(T t_) : t(t_) {}
private:
    void foo() override { std::cout << t; }
    T t;
};

Derived<int> make_obj()
{
    return Derived<int>(7);
}
Run Code Online (Sandbox Code Playgroud)

c++ templates template-instantiation

5
推荐指数
1
解决办法
199
查看次数

标签 统计

c++ ×1

template-instantiation ×1

templates ×1