Forward declaration of template class nested inside template class

我怕爱的太早我们不能终老 提交于 2019-12-19 07:55:14

问题


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 ? 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 ?


回答1:


Try the following

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

According to the C++ Standard (14.5.2 Member templates)

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.



来源:https://stackoverflow.com/questions/31341917/forward-declaration-of-template-class-nested-inside-template-class

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