Template function requires existence of inner class in non-templated class [duplicate]

天涯浪子 提交于 2019-12-25 08:03:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!