A typedef template without re-configuring the template. Use the template class parameter as a template

I want to do something like this:

template<class T> class BaseSubscriber {}; template<class T> class BasePublisher { // not working : invalid use of template-name 'BaseSubscriber' without an argument list typedef BaseSubscriber SubscriberType; // compiling typedef BaseSubscriber<T> SubscriberTypeT; }; template< template<class T> class Subscriber, class Data > class ClassA: public Subscriber<Data> { }; template< template<class T> class Publisher, class Data > class ClassB: public Publisher<Data> { // Ok, but I want that the type "BaseSubscriber" depends on the template parameter Publisher void method1(ClassA<BaseSubscriber, Data>&); // I want something like that. But how to define SubscriberType ? void method2(ClassA<Publisher<Data>::SubscriberType, Data>&); // or (SubscriberType only depends on the Publisher, nor on the Data) void method2(ClassA<Publisher::SubscriberType, Data>&); // Error : template argument is invalid void method3(ClassA<Publisher::SubscriberTypeT, Data>&); }; 

Is it possible to define some SubscriberType that I can use for the classA template classA ? Or is there any work?

If possible, I want to keep the prototype classA . I do not want to change it in

 template<class TSubscriber > classA {}; 

And I can not use C ++ 11. Thank you very much for your answers.

+4
source share
3 answers

This is a bit confusing what you mean when you say:

 // I want something like that. But how to define SubscriberType ? void method2(ClassA<Publisher::SubscriberType>); 

Since Publisher is a template, but you are not passing any arguments to it.

Anyway, here are a few options:

In C ++ 11, you can use template aliases:

 template<class T> class BasePublisher { template<typename U> using SubscriberType = BaseSubscriber<U>; }; 

You can also use nested classes:

 template<class T> class BaseSubscriber {}; template<class T> class BasePublisher { template<class U> class BaseSubscriber {}; }; 

Or change ClassA to use a type member:

 template<class T> class BasePublisher { template<class U> struct SubscriberType { typedef BaseSubscriber<U> type; }; }; template< template<class T> class SubscriberT > class ClassA { typedef typename SubscriberT::type Subscriber; }; 

Or sometimes inheritance works if you don't need an alias:

 template<class T> class BasePublisher { template<class U> struct SubscriberType : BaseSubscriber<U> {}; }; 
+3
source

Template aliases are not allowed in C ++ 03, although they are in C ++ 11.

Your goal is to be able to:

 typename BaseSubscriber<A>::SubscriberType<B> 

in C ++ 03 you should use a nested class like this:

 template< typename T > struct BasePublisher { template< typename U > struct Subscriber { typedef BaseSubcriber<U> type; }; }; 

and now you can do typename BasePublisher :: Subscriber :: type;

In C ++ 11, the syntax will look like this:

 template < typename T > struct BasePublisher { template< typename U > using SubscriberType = BaseSubscriber<U>; }; 

there are not many compilers yet. although, since you cannot use typename typename, it may need another typedef.

+1
source

If I get it right, you want ClassA to get an instance of BaseSubscriber , such as BaseSubscriber<my_tag> and ClassB , when creating BasePublisher . it is right?

If your answer is yes, then why do you declare the template argument of the template ClassA and ClassB as a template, you do not want to do something with it in ClassA or ClassB , so it should be a non-template class:

 template<class T> class BasePublisher { // compiling typedef BaseSubscriber<T> SubscriberTypeT; }; template< class SubscriberT > class ClassA : SubscriberT {}; template< class PublisherT > class ClassB : PublisherT { void method1( ClassA<typename PublisherT::SubscriberTypeT> ); }; 
0
source

Source: https://habr.com/ru/post/1444546/


All Articles