声明朋友时必须声明类键

发布于 2024-07-15 01:31:09 字数 613 浏览 9 评论 0原文

当我这样声明友元时,g++ 编译器会抱怨此错误:

friend MyClass;

而不是

friend class MyClass;

Why should the class keywords be required? (Borland C++ 编译器,顺便说一句,不需要它。)

编译器不能简单地在符号表中查找 MyClass 并告诉它被声明为一个类吗? (显然它无论如何都会进行查找,因为当 MyClass 未声明时它会抱怨)

它不像是在对类进行前向声明:我仍然必须声明上面的类,或者至少要前向声明它。

如果需要的话进行前向声明,这对我来说是有意义的(实际上会很棒)

friend class MyClass;

,否则对我来说它就像语法盐。

近 20 年来,我一直愉快地使用不带 classstruct 关键字的 friend 语句,没有编译器抱怨。 这是相当新的东西吗?

The g++ compiler complains with this error when I declare a friend thusly:

friend MyClass;

instead of

friend class MyClass;

Why should the class keyword be required?
(the Borland C++ compiler, BTW, does not require it.)

Couldn't the compiler simply look-up MyClass in the symbol table and tell it was declared as a class? (it is obviously doing the look-up anyway because it complains when MyClass it not declared)

It is not like it is making a forward declaration of the class: I still have to have either declared the class above or at least have forward declared it.

It would make sense to me (would be great actually) if

friend class MyClass;

makes a forward declaration if needed, otherwise it just seems like syntactic salt to me.

I have been merrily using friend statements without the class or struct keyword with no compiler complaints for almost 20 years.
Is this something fairly new?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪梦之恋 2024-07-22 01:31:09

我对此感到惊讶(因此删除了之前的错误答案)。 C++03标准在11.4中说:

在类的友元声明中应使用详细类型说明符。

然后为了确保没有误解,它在脚注中添加了以下内容:

需要详细类型说明符的类键。

GCC 是我拥有的唯一一个抱怨缺少类键的编译器,但看起来其他编译器让我们摆脱了一些非标准的东西......

现在至于理由 - 你必须问某人关于编译器(或标准)的了解比我多。

I was surprised about this (and as a result deleted a previous incorrect answer). The C++03 standard says in 11.4:

An elaborated-type-specifier shall be used in a friend declaration for a class.

Then to make sure there's no misunderstanding, it footnotes that with:

The class-key of the elaborated-type-specifier is required.

GCC is the only compiler that I have that complains about the missing class-key, but it looks like other compilers are letting us get away with something non-standard...

Now as for the rationale - you'd have to ask someone who knows more about compilers (or standards) than I do.

请远离我 2024-07-22 01:31:09

对于您的问题,因为这是 ISO/IEC 14882:2003 指定的方式(第 7.1.4 节)。 friend 构造本质上指定为:

friend <declaration>

其中 是类、结构、模板或函数的有效声明。

因此,

MyClass;

不是有效的声明,而:

class MyClass;

或:

struct MyClass;

是。

同上,相应地:

friend class MyClass;

friend struct MyClass;

To the point of your question, because it is the way ISO/IEC 14882:2003 specifies it (section 7.1.4). The friend construct is essentially specified as:

friend <declaration>

where <declaration> is the valid declaration of a class, struct, template, or function.

Thus,

MyClass;

is not a valid declaration, whereas:

class MyClass;

or:

struct MyClass;

are.

Idem for, correspondingly:

friend class MyClass;

or

friend struct MyClass;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文