How to name a nested template in a template of a base class?

In the following setup, how can I do this so that I can refer to the name Bar inside the Derived<T> derived class?

 template <typename T> struct Foo { template <typename U> struct Bar { }; }; template <typename T> struct Derived : Foo<T> { // what goes here? Bar<int> x; // Error: 'Bar' does not name a type }; 

I tried using Foo<T>::Bar; but it doesn’t help. Is there any using declaration that can make the name of the nested base template known to the derived class so that I can keep simple de & shy; cla & shy; ration Bar<int> x ?

I know I can say typename Foo<T>::template Bar<int> x; , but I have many such cases, and I do not want to burden the code unnecessary with such verbosity. I also have many different " int s", so a typedef for each instance of the nested template is also impossible.

In addition, I cannot use GCC 4.7 at this stage, not C ++ 11, and thus you will like the “traditional” solution without template aliases.

+4
source share
3 answers

In C ++ 11, you can use an alias pattern:

 template <typename T> struct Derived : Foo<T> { template<typename X> using Bar = typename Foo<T>::template Bar<X>; Bar<int> x; }; 

Edit

The traditional solution is what you said, typename Foo<T>:template Bar<int> , or to emulate a typedef template

 template <typename T> struct Derived : Foo<T> { template<typename X> struct Bar { typedef typename Foo<T>::template Bar<X> type; }; typename Bar<int>::type x; }; 

One of the reasons for adding alias patterns to the language is the support for things that cannot be easily expressed in C ++ 03.

+6
source

Declaring x as Foo<T>::Bar<int> x; just works for me.

+1
source

It works:

 template <typename T> struct Foo { template <typename U> struct Bar { }; }; template <typename T> struct Derived : Foo<T> { template<class W> struct Bar : public Foo<T>::template Bar<W> { }; Bar<int> x; }; 

IDK, if this is what you are looking for, but it compiles.

0
source

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


All Articles