gpt4 book ai didi

c++ - 在几层继承上重载解析?

转载 作者:太空宇宙 更新时间:2023-11-04 15:58:59 25 4
gpt4 key购买 nike

考虑以下具有多层继承的示例:

struct A {
void operator()(double x);
};

struct B: A {
using A::operator();
template <class... T> void operator()(T... x);
};

struct C: B {
using B::operator();
void operator()() const;
void operator()(int x) const;
};

struct D: C {
using C::operator();
void operator()();
};

重载决议是否会像 D 被写成这样一样工作:

struct D {
void operator()(double x);
template <class... T> void operator()(T... x);
void operator()() const;
void operator()(int x) const;
void operator()();
};

或者相反,编译器尝试在 D 中找到工作重载,然后在 C 中,然后在 B 中,然后在一个?换句话说,继承在重载决策(对于不具有相同签名的函数)中是否起作用?

最佳答案

一般规则是重载决策将考虑通过名称查找找到的声明集,而不是其他。

根据 [namespace.udecl]/1:

Each using-declarator in a using-declaration introduces a set of declarations into the declarative region in which the using-declaration appears. The set of declarations introduced by the using-declarator is found by performing qualified name lookup (6.4.3, 13.2) for the name in the usingdeclarator, excluding functions that are hidden as described below.

因此,在 D 范围内查找 operator() 的名称,它找到了 D::operator() 以及using-declaration,必须在 C 的范围内递归查找 operator(),找到两个 C::operator () 以及 using-declaration 等等。所以,是的,在您的情况下,重载解析会将整套 operator() 视为候选者。

关于c++ - 在几层继承上重载解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49041864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com