C++11 中好友模板类的视觉C++错误

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

本文关键字:视觉 C++ 错误 好友 C++11      更新时间:2023-10-16

我有一段代码可以使用g ++/clang++。最近有人向我报告说,它与视觉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++ 返回以下错误:

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声明为好友会使程序格式不正确,因此不需要诊断。