嵌套在模板类中的模板类的前向声明 [英] 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 {};
现在,如果Outer本身是模板类,是否有办法将Inner的声明保留在Outer的声明之外?像这样的东西:
Now if Outer is itself a template class, is there a way to keep the declaration of Inner outside the declaration of 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 ?
推荐答案
尝试以下操作
template<typename T>
template <int N>
class Outer<T>::Inner {};
根据C ++标准(14.5.2成员模板)
According to the C++ Standard (14.5.2 Member templates)
1可以在类或类模板中声明模板;这样的 模板称为成员模板.可以定义成员模板 在其类定义或类模板定义之内或之外. A 在其外部定义的类模板的成员模板 类模板定义应使用 类模板的template-parameters,后跟 成员模板的template-parameters.
1 A template can be declared within a class or class template; 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.
这篇关于嵌套在模板类中的模板类的前向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!