考虑以下测试 1 代码
struct A {
private:
class face;
friend class face;
};
struct A::face {};
template <typename _CharT>
struct C : public A::face
{};
int main()
{
C<int> x;
}
这段代码是否形成良好?我在g ++和comeau下测试了它。 g ++编译好了,而comeau给出了以下错误信息(我认为是正确的)
"ComeauTest.c", line 12: error: class "A::face" (declared at line 9) is inaccessible
struct C : public A::face
^
detected during instantiation of class "C<_CharT> [with _CharT=int]"
at line 17
在这种情况下哪个编译器是正确的? Comeau是我所知道的最标准的符合标准的编译器之一。 g ++又错了吗?
(1)这不是现实生活中的代码。
答案 0 :(得分:6)
这是不正确的。 face
是私有的,因此无法从C访问。只有在C从A获得而不是face
时,这才是合法的。 face
是私人会员,因此friend
无效。