在C++

发布于 2025-02-05 07:27:50 字数 587 浏览 2 评论 0原文

在C ++中,一个类别在一个类A成员函数中与Ref-Qualifier无需参考函数的成员函数过载。但与此同时,可以从父类中继承一个成员函数,并在子类中过载,如示例:

struct A {
    void f() {}
    //void f() & {} //overload error everywhere
};

struct B : A {
    using A::f;
    void f() & {} //ok everywhere
};

int main() {
    B b;
    b.f(); //ok in GCC only
}

仅在f的调用期间,Clang抱怨呼叫成员函数“ f”是模棱两可的。但是GCC在没有任何错误的情况下接受该程序,演示: https://gcc.godbolt.org.godbolt.org/5zzbwcs/5zzbwcs977 < /a>

哪个编译器在这里?

In C++ one cannot overload in one class a member function with ref-qualifier with a member function without ref-qualifier. But at the same time it is possible to inherit one member function from a parent class and overload it in a child class as in the example:

struct A {
    void f() {}
    //void f() & {} //overload error everywhere
};

struct B : A {
    using A::f;
    void f() & {} //ok everywhere
};

int main() {
    B b;
    b.f(); //ok in GCC only
}

Only during the invocation of f, Clang complains that call to member function 'f' is ambiguous. But GCC accepts the program without any error, demo: https://gcc.godbolt.org/z/5zzbWcs97

Which compiler is right here?

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

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

发布评论

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

评论(1

疑心病 2025-02-12 07:27:50

GCC是正确的接受这一点,但情况最近发生了变化。当前的措辞是,在类忽略的(基本级)声明中使用dem> 是模棱两可的(从某种意义上说,这比解决过载更严格,部分是因为没有参数列表但是)与班上的其他声明。 void()void()&amp;成员在这个意义上是模棱两可的,因此bf仅找到b' s f,呼叫有效。

在上一篇文章(在撰写本文中,这意味着“已发布”)版本的标准版本,这两个功能都可以使用,因为&amp;将它们区分开(从某种意义上甚至更严格),这将是不仅使呼叫含糊不清(正如Clang所说),而且要完全不明显,因为检查了基本和衍生的级功能,以过载兼容性。

GCC is correct to accept this, but the situation changed recently. The current phrasing is that a using-declaration in a class ignores (base-class) declarations that would be ambiguous (in a sense that is more strict than for overload resolution, partly because there is no argument list yet) with other declarations in the class. void() and void() & members are ambiguous in this sense, so b.f finds only B’s f and the call is valid.

In previous (as of this writing, that means “published”) versions of the standard, both functions would be made available because the & distinguished them (in a sense that is even stricter), which would not only render the call ambiguous (as Clang says) but be ill-formed outright because the base- and derived-class functions were checked for overload compatibility which they lack.

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