Is there a way to inherit an alias pattern?

I have a template base class:

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>; }; 

I would like to use Component<T> in my derived template classes.

I tried to declare Component as my own class instead of System , but when I try to reference it, the compiler will return 'Component' was not declared in this scope.

Any suggestions?

Edit: Usage Used:

 template class<typename EntityManager> class MovementSystem : public System<EntityManager> { public: virtual void Update(EntityManager& entity_manager, double dt) { Component<Position> position_component; // I'd like to use Component<T> here. } }; 
+7
c ++ templates
source share
1 answer

In a derived class template (which is most likely what you mean) you can do

 using Component = typename System<T>::Component; 

where T is some suitable type in the derived class.


Turning to the template, you can do this:

 template< class U > using Component = typename System<T>::template Component<U>; 

as noted by dyp in the comment.

Details depend on the context, here is a specific example for your code given in the message edition:

 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; } 

There is no easy way to do this, sorry.

This is, of course, an area where the main language can support the programmer much better, avoiding repeating the name over and over.


The rationale for the language is that some System specializations may not necessarily determine the type of Component or available.

With using you enable the compiler to diagnose the absence of this type in front.

+5
source share

All Articles