Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 有没有办法继承别名模板?_C++_Templates - Fatal编程技术网

C++ 有没有办法继承别名模板?

C++ 有没有办法继承别名模板?,c++,templates,C++,Templates,我有一个模板基类: template<class EntityManager> class System { public: virtual void Update(EntityManager& entity_manager, double dt) = 0; protected: template<typename T> using Component = typename EntityManager::template Component<T>

我有一个模板基类:

template<class EntityManager>
class System {
public:
  virtual void Update(EntityManager& entity_manager, double dt) = 0;
protected:
  template<typename T> using Component = typename EntityManager::template Component<T>;
};

在派生类模板中(这很可能就是您的意思),您可以执行以下操作

using Component = typename System<T>::Component;
正如dyp在评论中指出的那样

详细信息取决于上下文,以下是文章编辑中给出的代码示例:

class ET
{
public:
    template< class Type >
    struct Component {};
};

template<class EntityManager>
class System {
public:
  virtual void Update(EntityManager& entity_manager, double dt) = 0;
protected:
  template<typename T> using Component = typename EntityManager::template Component<T>;
};

template< class EntityManager >
class MovementSystem : public System<EntityManager> {
public:
#ifndef DONTFIXIT
  template< class T > using Component = typename EntityManager::template Component< T >;
#endif

  virtual void Update(EntityManager& entity_manager, double dt) {
    Component<int> position_component; // I'd like to use Component<T> here.
  }
};

auto main() -> int
{
    MovementSystem< ET > ms;
}
class-ET
{
公众:
模板<类类型>
结构组件{};
};
模板
阶级制度{
公众:
虚拟无效更新(实体管理员和实体管理员,双dt)=0;
受保护的:
使用组件的模板=typename EntityManager::模板组件;
};
模板
类移动系统:公共系统{
公众:
#ifndef DONTFIXIT
模板使用组件=typename EntityManager::模板组件;
#恩迪夫
虚拟无效更新(实体管理员和实体管理员,双dt){
组件位置\u Component;//我想在这里使用组件。
}
};
auto main()->int
{
运动系统ms;
}

对不起,没有简单的方法可以做到这一点

这显然是一个核心语言有潜力更好地支持程序员的领域,避免反复重复一个名称


该语言的基本原理是,
系统
的某些专门化可能不一定定义组件类型或可访问类型


通过
使用
可以使编译器预先诊断缺少此类类型。

在查找像
组件这样的非限定id时,不会搜索依赖于模板参数的基类。因此,
组件
是继承的,但它在派生类模板专门化中的常规非限定查找中不可见。(因此,如果包含派生类的定义,它可能会很有用。)请记住这一点。一般来说,通过在组件类型本身上模板化系统并消除
EntityManager
(以及任何其他)任何东西,您可能最终会得到更好的ECB设计。这并不是直接回答你的问题,而是可以消除多余的东西,最终以更简单、更愉快的方式解决主要问题。不幸的是,我们在这里不能确定,因为我们不知道确切的问题。“这种语言的基本原理是,系统的某些专门化可能不一定定义组件类型或可访问的组件类型。”为什么这样做像
template struct Component:public EntityManager::template Component{}
在基类中产生相同的结果?@CharlesW:可能您还没有实例化派生类(您可以通过实例进行更多检查)。或者可能是使用Visual C++,它不正确地实现两阶段查找。不知道为什么在正确之前得到了投票。Hm.@CharlesW可以在名称空间范围内使用别名模板,类似于类型特征。例如,在名称空间范围内,
template using Component=typename C::template Component
template< class U > using Component = typename System<T>::template Component<U>;
class ET
{
public:
    template< class Type >
    struct Component {};
};

template<class EntityManager>
class System {
public:
  virtual void Update(EntityManager& entity_manager, double dt) = 0;
protected:
  template<typename T> using Component = typename EntityManager::template Component<T>;
};

template< class EntityManager >
class MovementSystem : public System<EntityManager> {
public:
#ifndef DONTFIXIT
  template< class T > using Component = typename EntityManager::template Component< T >;
#endif

  virtual void Update(EntityManager& entity_manager, double dt) {
    Component<int> position_component; // I'd like to use Component<T> here.
  }
};

auto main() -> int
{
    MovementSystem< ET > ms;
}