可以 C++ 方法名称是否由标头中的类名称限定?

发布于 2024-07-16 02:49:05 字数 438 浏览 11 评论 0 原文

简单的问题,这是有效的 C++:

class Foo
{
    void Foo::doSomething();
};

问题的要点:在类声明中,在方法名称之前重复使用类名和双冒号是否有效?

我在使用 g++ 4.2.3 编译执行此操作的代码时遇到问题。 在深入研究和更改代码之前,我希望在这里看到对描述语法的引用。 或者降级编译器; 这确实是使用 g++ 3.3.6 构建的。

我得到的错误(大致)是:

Foo.h:3: error: extra qualification ‘Foo::’ on member ‘doSomething’

我用谷歌搜索了,但无法想出一些东西。 我没有标准,即使有,也可能需要很长时间才能找到权威的东西。 我不是 C++ 语言律师。

Simple question, is this valid C++:

class Foo
{
    void Foo::doSomething();
};

The point of the question: is that repeated use of the class name and double colon before the method name valid inside the class declaration?

I'm having issues compiling code that does this using g++ 4.2.3. I would love to see a reference to something describing the syntax here, before digging in and changing the code. Or downgrading the compiler; this does build with g++ 3.3.6.

The error I'm getting is (roughly):

Foo.h:3: error: extra qualification ‘Foo::’ on member ‘doSomething’

I did Google, but couldn't come up with something. I don't have the standard, and even if I did it would probably take me quite a while to find anything authoritative. I'm no C++ language lawyer.

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

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

发布评论

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

评论(2

薄荷→糖丶微凉 2024-07-23 02:49:05

我查看了标准,第 9.2 节是相关部分。 我对 BNF 不太感兴趣,但我在 BNF 中没有看到任何针对班级成员的内容表明这是允许的。 该标识符在 BNF 中甚至被命名为“unqualified-id”。

G++ 改变了 4.1 版本中的行为,显然很多其他编译器都接受了这一点,但我从未见过使用这种风格,我不知道为什么有人会这样做。 由于它似乎不会在多种编译器上触发错误,因此这种风格可能有一些历史原因,但据我所知,它确实无效。

我通过 Google 找到的唯一好的参考是此页面,它只是尝试解释 G++ 4.1 中的一些更改。

I took a look at the standard, section 9.2 would be the relevant portion. I'm not that great with BNF but I didn't see anything in the BNF for class members that would indicate this was allowed. The identifier is even named "unqualified-id" in the BNF.

G++ changed the behavior in version 4.1, and apparently a lot of other compilers accepted this, but I've never seen this style used and I have no idea why anyone would do it. Since it seems to not trigger an error on a pretty wide variety of compilers, there may be some historical reason for this style, but as far as I can tell it's indeed not valid.

The only good reference I found through Google was this page, which just attempts to explain some of the changes in G++ 4.1.

颜漓半夏 2024-07-23 02:49:05

和丹一样,我查看了标准,但没有得到明确的结果。 我用 Comeau 的在线编译器(被认为是世界上最符合标准的)尝试了你的代码,并得到:

第 3 行:错误:限定名称不是
允许在成员声明中使用

如果您有兴趣进一步解决这个问题,我建议您在 comp.lang.c++.moderated 新闻组,因为那里的 C++ 语言律师比这里多得多。

Like Dan, I looked at the Standard without definitive results. I tried your code with Comeau's on-line compiler (considered the world's most standard compliant) and got:

line 3: error: qualified name is not
allowed in member declaration

If you are interested in taking this further, I suggest posting a question on the comp.lang.c++.moderated newsgroup, as there are a lot more C++ language lawyers there than there are here.

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