【发布时间】:2018-03-30 21:34:23
【问题描述】:
在 C++ 中使类模板 B 的基类规范依赖于类模板 B 的朋友类 A 的私有定义是否合法?示例代码:
struct Empty {};
template <typename T>
struct B;
struct A
{
friend struct B<A>;
private:
using Base = Empty;
};
template <typename T>
struct B : T::Base
{
};
int main()
{
B<A> test;
return 0;
}
天箭链接:https://godbolt.org/g/HFKaTQ
代码可以使用 Clang 主干(和旧版本)和 MSVC 19(VS 2017)编译,但无法使用 GCC 主干(和旧版本)编译:
test.cpp: In instantiation of 'class B<A>':
test.cpp:21:7: required from here
test.cpp:15:8: error: 'using Base = class Empty' is private within this context
struct B : T::Base
^
test.cpp:11:20: note: declared private here
using Base = Empty;
^
哪个编译器出错了?
编辑:顺便说一句,如果B 被转换为常规类(删除模板参数),代码将在 GCC 中编译。所以,我想它也应该在类模板案例中工作。 cppreference 还说:“朋友本身也可以从此类的私有成员和受保护成员继承。(C++11 起)”
【问题讨论】:
-
提交一份针对 GCC 的 [rejects-valid] 错误报告。他们很可能会承认这是一个错误,但也许他们会告诉你为什么他们认为它的格式不正确。
-
@Brian 是的,谢谢。我在过去几周已经这样做了,但忘记更新问题/提供答案。