C++11 中友元模板 class 的 Visual C++ 错误

Visual C++ error with friend template class in C++11

我有一段代码适用于 g++/clang++。最近有人向我报告说它与 Visual C++ 不兼容。

代码是这样的:

namespace q {
    template <typename X, typename Y>
    struct A {};
}

template <typename X>
struct B {
    template <typename Y>
    friend struct q::A;
};

int main() {
    return 0;
}

VC++ returns 出现如下错误:

source_file.cpp(9): error C2976: 'q::A': too few template arguments
source_file.cpp(3): note: see declaration of 'q::A'
source_file.cpp(10): note: see reference to class template instantiation 'B<X>' being compiled

谁是正确的?有没有一种便携的方法可以做到这一点?

正确编写模板参数应该会有所帮助:

template <typename X, typename Y>
friend struct q::A;

请注意,错误地将 A 声明为朋友会使程序格式错误,不需要诊断。