a class-key must be declared when declaring a friend

人盡茶涼 提交于 2019-11-27 13:57:25

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.

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;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!