gpt4 book ai didi

c++ - Comeau vs g++ [又一个错误]

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:26:18 27 4
gpt4 key购买 nike

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

最佳答案

这是不正确的。 face 是私有(private)的,所以它不能从 C 访问。只有当 C 是 A 的好友时,这才是合法的,而不是 faceface 是私有(private)成员,因此 friend 对它无效。

关于c++ - Comeau vs g++ [又一个错误],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8538067/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com