Johan
Johan

Reputation: 512

C++ templates and alias

This will be used in a library and not like the example, the example below is made such that it just explains the question.

I have a template class BaseType with a template specialization.

template<class...> 
class BaseType; //Forward declare

template<typename T>  
BaseType<T> { /* ...implementation 1... */ };

template<typename T, typename R>  
BaseType<T,R> { /* ...implementation 2... */ };

I would now create an alias for the shared_ptr<BaseType> templates. Doing it for one of the template versions works fine.

template<typename T>
using SlotType = std::shared_ptr<BaseType<T> >;

using EventArgSlot = SlotType<EventArgs>;

EventArgSlot slot1;

But how should I define the alias such that it also supports this:

using MessageIntegerSlot = SlotType<MessageArgs, int>;
MessageIntegerSlot slot2;

Just adding an additional alias with the same and an additional template parameter does not work:

template<typename T, typename R>
using SlotType = std::shared_ptr<BaseType<T,R> >;

Is this possible to solve in C++11/14?

Upvotes: 2

Views: 107

Answers (1)

songyuanyao
songyuanyao

Reputation: 173044

You could take advantage of parameter pack and change the definition of SlotType to

template<typename... T>
using SlotType = std::shared_ptr<BaseType<T...> >;

then

using EventArgSlot = SlotType<EventArgs>;              // use the 1st specialization of BaseType
using MessageIntegerSlot = SlotType<MessageArgs, int>; // use the 2nd specialization of BaseType

Upvotes: 6

Related Questions