模板功能要求在非模板类中存在内部类 [英] Template function requires existence of inner class in non-templated class

查看:84
本文介绍了模板功能要求在非模板类中存在内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个模板函数 f ,它需要其模板参数类型 T 具有一个名为 Inner 的内部类.

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

f 内部,应实例化 T :: Inner 类.

第一次尝试.

//
// "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;
}

那是为什么?

据我了解: Inner 被声明为类型. 该模板尚未实例化. 首先是否存在 Inner 类型 实例化时-未定义.我要去哪里错了?

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?

推荐答案

我想你想做

typename T::Inner i;

typedef typename T::Inner I;
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.

这篇关于模板功能要求在非模板类中存在内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆