c++ GCC不认为从基类引入的函数是不明确的

whhtz7ly  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(177)

下面的代码

  1. struct Foo{};
  2. struct Bar{};
  3. struct Base {
  4. Foo func1(const Foo , const Bar = Bar{}) const {
  5. return {};
  6. };
  7. };
  8. struct Derived : public Base {
  9. using Base::func1;
  10. Foo func1(const Foo ) const {
  11. return {};
  12. };
  13. };
  14. int main() {
  15. Foo foo;
  16. Derived der;
  17. der.func1(foo);
  18. }

被ICX和clang拒绝,但被GCC接受(最高13.1)。https://godbolt.org/z/4M3rrs3r4
我认为GCC错了。如果这两个func1是非成员,GCC将以不明确为由拒绝调用。
我说的对吗

juzqafwq

juzqafwq1#

这是编译器gcc的bug。
从C++ 20(9.9使用声明)
15 [* 注6*:为了在重载解析期间形成一组候选函数,由using-declaration引入到派生类中的函数被视为派生类的成员(11.8)。特别是,隐式对象参数被视为对派生类的引用,而不是对基类的引用(12.4.2)。这对函数的类型没有影响,并且在所有其他方面,函数仍然是基类的成员。- * 结束注解 *]
和(9.3.4.7默认参数)
1....当一个函数的声明通过using-declaration(9.9)引入时,任何与声明相关的默认参数信息也是已知的。
因此,对于重载解析,类Derived中的候选函数集由两个函数组成

  1. Foo func1(const Foo , const Bar = Bar{}) const {
  2. return {};
  3. }

  1. Foo func1(const Foo ) const {
  2. return {};
  3. }

而这样的号召

  1. der.func1(foo);

在这种情况下是模糊的。可以调用任一函数。

展开查看全部
pokxtpni

pokxtpni2#

这是GCC 7中引入的一个bug:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82894

相关问题