5

考虑以下测试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) 这不是现实生活中的代码。

4

1 回答 1

6

这是不正确的。face是私有的,所以它不能从 C 访问。这只有在 C 是 A 的朋友时才合法,而不是face. face是一个私有成员,所以friend它没有效果。

于 2011-12-16T17:52:46.777 回答