How to define a friend function declared in a non template class internal to a template class outside of both classes?

Viewed 316

I found "how to define a friend template function of a template class outside of its declaration" (SO/cppreference), but how to do that if we add another internal non template class in the mix?

I.e. how to (externally) define operator<< declared in class Internal from the following example:

#include <iostream>

template <typename T>
class External {
public:
    explicit External(T initial) : value{initial} {}
    class Internal {
    public:
        Internal(const External& e) : internal_value{e.value} {}

    private:        
        friend std::ostream& operator<<(std::ostream& os, const Internal& i);
        // ^^^ this one
        /* body
        {
            return os << i.internal_value;
        }
        */

        T internal_value;
    };

    friend std::ostream& operator<<(std::ostream& os, const External& e)
    {
        return os << Internal{e};
    }
private:
    T value;
};

int main()
{
    std::cout << External<int>{5};
}
1 Answers
Related