简体   繁体   English

嵌套在模板类中的模板类的前向声明

[英]Forward declaration of template class nested inside template class

You can forward declare a template inner class inside a normal class, and use the defined type as any other forward declared type. 您可以在普通类中向前声明模板内部类,并将定义的类型用作任何其他向前声明的类型。

class Outer {
    template <int N> class Inner;
    typedef Inner<0> Inner0;
    Inner0* f();
};

template<int N>
class Outer::Inner {};

Now if Outer is itself a template class, is there a way to keep the declaration of Inner outside the declaration of Outer ? 现在,如果Outer本身是模板类,是否有办法将Inner的声明保留在Outer的声明之外? Something like : 就像是 :

template<typename T>
class Outer {
    template <int N> class Inner;
    typedef Inner<0> Inner0;
    Inner0* f();
};

template<typename T, int N> //This won't work
class Outer<T>::Inner {};

Is there a correct syntax to declare Outer with the right template parameters ? 是否有正确的语法使用正确的模板参数声明Outer?

Try the following 尝试以下

template<typename T>
template <int N>
class Outer<T>::Inner {};

According to the C++ Standard (14.5.2 Member templates) 根据C ++标准(14.5.2成员模板)

1 A template can be declared within a class or class template; 1可以在类或类模板中声明模板; such a template is called a member template. 这样的模板称为成员模板。 A member template can be defined within or outside its class definition or class template definition. 可以在其类定义或类模板定义之内或之外定义成员模板。 A member template of a class template that is defined outside of its class template definition shall be specified with the template-parameters of the class template followed by the template-parameters of the member template. 在类模板定义之外定义的类模板的成员模板,应使用类模板的模板参数和成员模板的模板参数来指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM