在 class 中声明模板 class,其类型与父 class 相同

declare template class within class with same types as parent class

class 范围内 class 的花式 cout/shift operator<<

template <typename X, typename Y> class Foo {
public:
    template <typename A, typename B> class Bar {
        A a;
        B b;
    };

    Bar<X,Y> baba;
};

template <typename V, typename W>
std::ostream &operator<< (std::ostream& os, Foo<V,W>::template Bar<V,W> *b) {return os;}

这不编译。但是如何正确地做呢?


错误:

error: expected template-id for type before ‘*’ token
  std::ostream &operator<<(std::ostream &os, Foo<V, W>::template Bar<V, W> *b)

应该是:

template <typename V, typename W>
std::ostream &operator<< (std::ostream& os, typename Foo<V,W>::template Bar<V,W> &b) {return os;}

但更改 class 可能更简单:

template <typename X, typename Y> class Foo {
public:
    class Bar {
        X a;
        Y b;
    };

    Bar baba;
};

template <typename V, typename W>
std::ostream &operator<< (std::ostream& os, const typename Foo<V,W>::Bar& b) {return os;}

真正的解决方案(避免类型推导问题)是将自由函数放在 class:

template <typename X, typename Y> class Foo {
public:
    class Bar {
        X a;
        Y b;

        friend
        std::ostream &operator<< (std::ostream& os, const Bar &b) {return os;}
    };

    Bar baba;
};