如何将模板函数声明为模板嵌套 class 的朋友?

How to declare a template function a friend of a templated nested class?

如何使 get 成为可以访问 outer<T>::inner<U> 的私有构造函数的封闭作用域中的函数?

template <typename T>
struct outer {
    template <typename U>
    class inner {
        inner() {}
    public:
        friend inner get(outer&) {
            return {};
        }
    };
};


int main() {
    outer<int> foo;
    outer<int>::inner<float> bar = get<float>(foo);
}

我曾尝试通过使 inner 具有 template <typename V, typename W> friend inner<V> get(outer<W>&); 来将其从 class 中声明出来,但这也没有用。

I have tried declaring it out of class by making inner have a template <typename V, typename W> friend inner<V> get(outer<W>&);

需要在友元声明之前声明模板函数,告诉编译器get是一个模板。例如

// definition of outer
template <typename T>
struct outer {

    // forward declaration of inner
    template <typename U>
    class inner;
};

// declaration of get
template <typename V, typename W> 
typename outer<W>::template inner<V> get(outer<W>&);

// definition of inner
template <typename T>
template <typename U>
class outer<T>::inner {
    inner() {}
    public:
    // friend declaration for get<U, T>
    friend inner<U> get<U>(outer<T>&);
};

// definition of get
template <typename V, typename W> 
typename outer<W>::template inner<V> get(outer<W>&) {
    return {};
}

LIVE