成员函数声明中是否允许限定名称?

发布于 2024-11-30 16:10:15 字数 108 浏览 5 评论 0原文

该代码被MSVC9.0接受。我的问题是根据标准(旧的和/或新的)它是否合法。也非常欢迎报价。

class X
{
   void X::f();
};

This code is accepted by MSVC9.0. My question is whether it is legal according to the standard (the old and/or the new one). A quote would be very much welcome, too.

class X
{
   void X::f();
};

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

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

发布评论

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

评论(2

迷鸟归林 2024-12-07 16:10:15

不,这是无效的。这里,X::f 是一个限定名称;您正尝试将其用作declarator-id。 C++03 8.3[dcl.meaning]/1 列出了可以限定 declarator-id 的情况:

declarator-id 不得被限定,除非

  • 外部定义成员函数或静态数据成员
    同类产品中,

  • 命名空间外部的函数或变量成员的定义或显式实例化
    其命名空间,或

  • 先前在其命名空间之外声明的显式专业化的定义,或

  • 作为另一个类或命名空间成员的友元函数的声明。

因为 X::f 不属于这四个类别中的任何一个,所以它是不正确的。

要求类定义之外的成员函数定义被限定的规则可以在 C++03 9.3[class.mfct]/5 中找到:

如果成员函数的定义在词法上位于其类定义之外,则应使用 :: 运算符通过其类名来限定成员函数名称。

No, this is not valid. Here, X::f is a qualified name; you are attempting to use it as a declarator-id. C++03 8.3[dcl.meaning]/1 lists the circumstances under which a declarator-id may be qualified:

A declarator-id shall not be qualified except for

  • the definition of a member function or static data member outside
    of its class,

  • the definition or explicit instantiation of a function or variable member of a namespace outside
    of its namespace, or

  • the definition of a previously declared explicit specialization outside of its namespace, or

  • the declaration of a friend function that is a member of another class or namespace.

Because X::f falls into none of these four categories, it is incorrect.

The rule that requires the definition of a member function outside of the class definition to be qualified can be found at C++03 9.3[class.mfct]/5:

If the definition of a member function is lexically outside its class definition, the member function name shall be qualified by its class name using the :: operator.

左岸枫 2024-12-07 16:10:15

据我了解,根据 C++03 规范,它无效

参考 - C++03 标准:

第 $8.3 节:

每个声明符仅包含一个声明符 ID;它命名所声明的标识符。除了一些特殊函数的声明(12.3、12.4、13.5)以及模板特化或部分特化(14.7)的声明外,声明符 id 的 id 表达式应是一个简单标识符。 declarator-id 不得被限定,除非在其类之外定义了成员函数 (9.3) 或静态数据成员 (9.4) 或嵌套类 (9.7),其名称空间之外的名称空间的函数、变量或类成员,或先前在其名称空间之外声明的显式特化的定义,或作为另一个类或名称空间的成员的友元函数的声明 (11.4)。< /em>

我希望我能得出上述内容的适当含义。我承认阅读&理解标准中的引文让我有点头晕。如果我解释错误请告诉我。

As I understand it is Not valid as per the C++03 Specification.

Reference - C++03 standard:

Section $8.3:

Each declarator contains exactly one declarator-id; it names the identifier that is declared. The id-expression of a declarator-id shall be a simple identifier except for the declaration of some special functions (12.3, 12.4, 13.5) and for the declaration of template specializations or partial specializations (14.7). A declarator-id shall not be qualified except for the definition of a member function (9.3) or static data member (9.4) or nested class (9.7) outside of its class, the definition or explicit instantiation of a function, variable or class member of a namespace outside of its namespace, or the definition of a previously declared explicit specialization outside of its namespace, or the declaration of a friend function that is a member of another class or namespace (11.4).

I hope I am deriving the appropriate meaning of the above. I will admit reading & understanding the quotes from the Standard makes me a little dizzy. Let me know if I interpret it wrongly.

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