【发布时间】:2026-01-22 04:00:01
【问题描述】:
我见过以下 C++11 之前的代码,用作声明类模板朋友的技巧(在 C++11 中可以简单地使用 friend T; 完成)
template <typename T>
struct Wrapper
{
typedef T type;
};
template <typename T>
class Foo
{
friend class Wrapper<T>::type; // effectively makes T a friend
};
struct Test{};
int main()
{
Foo<Test> foo;
}
代码在 g++ (4.9/5.1/6) 上编译良好,但在 clang++ (3.5/3.6/3.7) 下编译失败并出现错误
错误:详细类型指的是 typedef
朋友类 Wrapper::type;
上述代码是否符合标准,即有效与否?
【问题讨论】:
标签: c++ templates language-lawyer friend