成员函数的const修饰符如何影响重载解析? [英] How does const modifier for member functions affect overload resolution?

查看:63
本文介绍了成员函数的const修饰符如何影响重载解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下测试代码:

#include <string>
#include <iostream>

class CString
{
public:
    CString(char const*) {}
};

class TestBed
{
public:
    void Comparison(CString const&) { std::cout << "CString Overload" << std::endl; }
    void Comparison(std::string const&) { std::cout << "std::string overload" << std::endl; }
};

int main()
{
    TestBed tb;
    tb.Comparison("Hello World");
}

此代码无法编译,因为对 Comparison()的调用不明确.我希望这种行为.

This code fails to compile because the call to Comparison() is ambiguous. I expect this behavior.

但是,当我使 Comparison()中的任何一个重载 const 时,例如: void Comparison(std :: string const&)const void比较(CString const&)const (但不能同时使用两者),代码将编译并选择非const重载.

However, when I make either of the Comparison() overloads const, as in: void Comparison(std::string const&) const or void Comparison(CString const&) const (but not both), the code compiles and chooses the non-const overload.

重载解析规则非常复杂,我还没有看到任何描述 const 如何影响这种情况的内容.我的理解是:

Overload resolution rules are pretty complex and I haven't seen anything that describes how const affects this situation. My understanding is:

  1. 首先选择参数完全匹配的函数
  2. 接下来尝试1级隐式转换

在两种情况下均为1&2是模棱两可的.有人可以解释吗?谢谢.

In both cases, 1 & 2 are ambiguous. Can someone explain this? Thanks.

推荐答案

对于类方法,将 this 部分视为多余的参数.因此,如果您将 CString 设置为一个常量,则将设置重载:

For class methods, the this part is considered as if it were an extra argument. So if you made the CString one const, that makes the overload set:

Comparison(const TestBed&, CString const&) // (1)
Comparison(TestBed&, std::string const&)   // (2)

对于(1),我们需要进行两次转换: const 转换和到 CString 的转换.但是对于(2),我们只需要执行一次转换即可:转换为 std :: string .因此,首选(2).

For (1), we need to do two conversions: a const conversion, and a conversion to CString. But for (2), we only need to do a single conversion: to std::string. Thus, (2) is preferred.

我们可以通过添加第三个函数对 this 进行一次转换来验证这一点:

We can verify this by adding a third function that does one single conversion for this:

Comparison(const TestBed&, const char*)  // (3)

在这里,我们再次只有一个转换(在"first"参数中),因此重载集是模棱两可的.

Here, we again only have a single conversion (in the "first" argument), and thus the overload set is ambiguous.

在[over.match.funcs]中:

In [over.match.funcs]:

成员函数被认为具有一个额外的参数,称为隐式对象参数,该参数表示已为其调用成员函数的对象.出于重载解析的目的,静态和非静态成员函数均具有隐式对象参数,而构造函数则没有.

a member function is considered to have an extra parameter, called the implicit object parameter, which represents the object for which the member function has been called. For the purposes of overload resolution, both static and non-static member functions have an implicit object parameter, but constructors do not.

对于非静态成员函数,隐式对象参数的类型为

For non-static member functions, the type of the implicit object parameter is

-对于不带ref限定符或带&声明的函数的对cv X的左值引用"裁判限定符

— "lvalue reference to cv X" for functions declared without a ref-qualifier or with the & ref-qualifier

-用&&声明的函数对cv X的右值引用"裁判限定符

— "rvalue reference to cv X" for functions declared with the && ref-qualifier

其中X是该函数是成员的类,而cv是该成员上的cv限定词函数声明.[示例:对于类X的const成员函数,额外的参数假定为的类型为对const X的引用".—完示例]

where X is the class of which the function is a member and cv is the cv-qualification on the member function declaration. [ Example: for a const member function of class X, the extra parameter is assumed to have type "reference to const X". —end example ]

在重载解析期间,隐含对象参数与​​其他参数没有区别.

During overload resolution, the implied object argument is indistinguishable from other arguments.

这确定了为什么我们考虑 const TestBed& TestBed& .然后只需比较重载(1)(2)之间的转换顺序即可.对于第二个参数,两个转换顺序是相等的,但是对于第一个参数,(2)具有更好的转换顺序(即精确),这就是为什么它毫无歧义地获胜的原因.

That establishes why we consider const TestBed& vs TestBed&. And then it's just a matter of comparison the conversion sequences between the overloads (1) and (2). For the 2nd argument, both conversion sequences are equal, but for the 1st argument, (2) has a better conversion sequence (namely Exact) - which is why it wins without ambiguity.

这篇关于成员函数的const修饰符如何影响重载解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆