繁体   English   中英

多态 function object 不能用作模板 function 中的类型,不允许使用类型名称

[英]Polymorphic function object not working as a type in a template function, type name is not allowed

我正在尝试在使用 ECS 时在 opengl 中实现实例化渲染。 作为一种解决方案,我认为:不要为每个实体调用绘图 function,而是为每种几何类型调用绘图 function。 为此,我想提供驻留在几何特定命名空间中的多态绘图函子,例如 TorusGeom 命名空间。

所以我有一个std::map< std::type_index, std::unique_ptr<DrawFunctor> >

并添加一个特定的绘图函子,我有

template <typename T>
void addFunctor() const            // new entry iff map doesn't yet contain functor of type 
     {
         static_assert(std::is_base_of<DrawFunctor, T>::value, "invalid typename, not a DrawFunctor");
         using std::type_index;
         type_index functorType = type_index(typeid(T));
         if (functors.find(functorType) == functors.end()) 
             functors[functorType] = std::make_unique<T>(new T()); 
     }

但是当我尝试调用 addFunctor

entity->manager.addFunctor< TorusGeom::TorusDraw >();
                            ~~~~~~~~~~~~~~~~~~~~ 

该行给出了type name not allowed. 我不明白为什么。 如果它是定义函子的方式:

struct DrawFunctor {
    virtual void operator()() = 0;
    virtual ~DrawFunctor() = default;
protected:
    DrawFunctor() {}
};
struct TorusDraw : public DrawFunctor {
    TorusDraw() : DrawFunctor() {} 
    void operator()() override final {
        glDrawElementsInstanced(...));
    }
};

我在Entity之前缺少class Manager的前向声明,感谢评论中的建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM