The template function requires an inner class in an unempiried class

There is a template function f that requires its template parameter type T to have an inner class named Inner.

Inside f, an instance of the T :: Inner class is created.

Try it first.

// // "error: need 'typename' before 'T:: Inner' because 'T' is a dependent scope" // template <typename T> void f( void ) { T::Inner i; } 

I get this, so here is the second attempt, where I do not understand what is wrong:

 /// "error: expected ';' before 'i' template<typename T> void f ( void ) { typename T::Inner I; I i; } 

Why?

In my understanding: Inner is declared as a type. The template has not yet been created. Regardless of whether the Inner type exists or not in the first instance, instantiation is not a definition. Where am I wrong?

0
c ++ definition templates nested
Jan 25 '17 at 0:07
source share
1 answer

I think you want to do

 typename T::Inner i; 

or

 typedef typename T::Inner I; I i; 

whereas what you have in the question actually declares the I variable, and then immediately tries to use it, as if it were a type.

+2
Jan 25 '17 at 0:09
source share



All Articles