使用基成员函数的重载决议引入派生 class
the overload resolution of using base member function introduced into derived class
根据标准中的一些引述:
[over.match.funcs]/4
[...] For non-conversion functions introduced by a using-declaration into a derived class, the function is considered to be a member of the derived class for the purpose of defining the type of the implicit object parameter.
并考虑以下代码:
#include <iostream>
struct Base {
void show(double) {
std::cout << "0" << std::endl;
}
};
struct Test :Base {
using Base::show; //#1
void show(double) { //#2
std::cout << "1" << std::endl;
}
};
int main() {
Test t;
t.show(1.2);
}
根据我引用的标准,意思是将Test
类型的隐式对象参数作为#1
类型的隐式对象参数。
对于 #1
,声明将是 show(Test&,double)
.
对于 #2
,声明将是 show(Test&,double)
。
所以为了重载决议,#1
的每个隐式转换序列与#2
的隐式转换序列没有区别,t.show(1.2)
的调用会产生歧义,但是#2
被调用,为什么?如果我在标准中遗漏了什么,请纠正我。
Test::show(double)
与 Base::show(double)
具有相同的签名;它只是 隐藏了 基础 class.
(强调我的)
If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.
根据标准,[namespace.udecl]/14
When a using-declarator brings declarations from a base class into a
derived class, member functions and member function templates in the
derived class override and/or hide member functions and member
function templates with the same name, parameter-type-list
([dcl.fct]), trailing requires-clause (if any), cv-qualification, and
ref-qualifier (if any), in a base class (rather than conflicting).
Such hidden or overridden declarations are excluded from the set of
declarations introduced by the using-declarator. [ Example:
struct B {
virtual void f(int);
virtual void f(char);
void g(int);
void h(int);
};
struct D : B {
using B::f;
void f(int); // OK: D::f(int) overrides B::f(int);
using B::g;
void g(char); // OK
using B::h;
void h(int); // OK: D::h(int) hides B::h(int)
};
...
根据标准中的一些引述:
[over.match.funcs]/4
[...] For non-conversion functions introduced by a using-declaration into a derived class, the function is considered to be a member of the derived class for the purpose of defining the type of the implicit object parameter.
并考虑以下代码:
#include <iostream>
struct Base {
void show(double) {
std::cout << "0" << std::endl;
}
};
struct Test :Base {
using Base::show; //#1
void show(double) { //#2
std::cout << "1" << std::endl;
}
};
int main() {
Test t;
t.show(1.2);
}
根据我引用的标准,意思是将Test
类型的隐式对象参数作为#1
类型的隐式对象参数。
对于 #1
,声明将是 show(Test&,double)
.
对于 #2
,声明将是 show(Test&,double)
。
所以为了重载决议,#1
的每个隐式转换序列与#2
的隐式转换序列没有区别,t.show(1.2)
的调用会产生歧义,但是#2
被调用,为什么?如果我在标准中遗漏了什么,请纠正我。
Test::show(double)
与 Base::show(double)
具有相同的签名;它只是 隐藏了 基础 class.
(强调我的)
If the derived class already has a member with the same name, parameter list, and qualifications, the derived class member hides or overrides (doesn't conflict with) the member that is introduced from the base class.
根据标准,[namespace.udecl]/14
When a using-declarator brings declarations from a base class into a derived class, member functions and member function templates in the derived class override and/or hide member functions and member function templates with the same name, parameter-type-list ([dcl.fct]), trailing requires-clause (if any), cv-qualification, and ref-qualifier (if any), in a base class (rather than conflicting). Such hidden or overridden declarations are excluded from the set of declarations introduced by the using-declarator. [ Example:
struct B { virtual void f(int); virtual void f(char); void g(int); void h(int); }; struct D : B { using B::f; void f(int); // OK: D::f(int) overrides B::f(int); using B::g; void g(char); // OK using B::h; void h(int); // OK: D::h(int) hides B::h(int) }; ...