问题
There's a template function f that requires its template parameter type T to have an inner class named Inner.
Inside f the class T::Inner shall be instantiated.
First try.
//
// "error: need 'typename' before 'T:: Inner' because 'T' is a dependent scope"
//
template <typename T>
void f( void )
{
T::Inner i;
}
I get that, so here comes the second try, where I don't get what's wrong:
/// "error: expected ';' before 'i'
template<typename T>
void f ( void )
{
typename T::Inner I;
I i;
}
Why is that?
In my understanding: Inner is declared as type. The template has not yet been instantiated. Whether the type Inner exists or not first becomes relevant on instantiation - not definition. Where am I going wrong?
回答1:
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 I
to be a variable, and then right after that you are trying to use it as though it's a type.
来源:https://stackoverflow.com/questions/41841195/template-function-requires-existence-of-inner-class-in-non-templated-class