Search code examples
c++templateslanguage-lawyerclass-template

Can we have an out of class definition for a class template that is a member of a class template


I know that the following snippet is valid:

template<typename T>
struct Custom 
{
    template<typename V> void func();
};

//valid
template<typename T> 
template<typename V>
void Custom<T>::func()
{
    
}

But can we do the same for a class template instead of a member function template. Take for example,

template<typename T>
struct Custom 
{   //declaration here
    template<typename V> struct InnerCustom;
};

//can we define the class template `InnerCustom` outside?
template<typename T> 
template<typename V>
Custom<T>::InnerCustom
{
    
};

My question is that can we have an out of class definition for the class template member InnerCustom just like we had for the member function template func? If so, how can i do that. Also, if not what is the reason for this difference.


Solution

  • Yes, you can make an out of class definition for InnerCustom:

    template <typename T>
    template <typename V>
    struct Custom<T>::InnerCustom { // note: `struct` added
        // ...
    };