用于const成员函数的重载解析C ++ [英] Overload resolution C++ for const member functions

查看:150
本文介绍了用于const成员函数的重载解析C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个类T,有两个成员函数

Assume you have a class T with two member functions


  1. char foo }

  2. char foo(){...}

  1. char foo() const {...}
  2. char foo() {...}.

这是我的理解,当调用一个常数T时,我们决定为(1);对于非常数T,我们决定为(2)。

It is my understanding that when called for a constant T, we resolve to (1); and for a non-constant T, we resolve to (2). 


  1. 是正确的吗?

  2. 在此分辨率中调用了哪个规则? (参考标准很棒,但有助于简要总结)

注意:


  1. 我试图谷歌的,但是老的命中我得到了SO是其他重载决议涉及const的情况。然而,链接到旧的SO实际上解释上述显然伟大。

  1. I tried to google for it, but old hits I got on SO were cases for other overload resolutions involving const. However, link to an old SO actually explaining the above obviously great.     

重新阅读Stroustrup的C ++编程语言,第2版(特别版),第11.12节中的String / Cref示例, p。由于Stroustrup是如此精确,答案可能在前面的部分,但我不知道在哪里。参考Stroustrup中的部分非常欢迎(第2版最好的,因为这是我有一个)。第10.2.6节将const成员引入那些不改变对象的值,这暗示了答案,但不是作为一个明确的解决方案指令。

This came up when re-reading Stroustrup's "The C++ programming language", 2nd edition ("Special Edition"), String/Cref example in section 11.12, p. 296. As Stroustrup is so precise, the answer might be in previous sections, but I fail to see where. Reference to sections in Stroustrup very welcome too (2nd edition best as this is the one I have). Section 10.2.6 introduces const members as those "that don't change an object's value", which hints at the answer, but doesn't strike me as a clear resolution directive.


推荐答案

在N3242(我手头的标准草案)中,13.3.1第4段说

In N3242 (the standard draft I have on hand), 13.3.1 paragraph 4 says


对于没有$ b声明的[非静态成员]函数,隐式对象参数的类型是对于 cv X的lvalue引用$ b ref-qualifier或者& ref-qualifier

the type of the implicit object parameter is "lvalue reference to cv X" for [non-static member] functions declared without a ref-qualifier or with the & ref-qualifier

这意味着隐式对象参数的类型,是 cv X 的值的引用,其中 X 是类, cv 是成员变量(即const或非const)的cv-qualification。然后,重载解析继续正常。

this means that type of the implicit object argument, which occurs first, is an "lvalue reference to cv X", where X is the class, and cv is the cv-qualification of the member variable (i.e. const or non-const). Then, overload resolution continues as normal.

重载解析过程,首先,它们都被列为候选函数,因为它们在正确的范围内并具有正确的名称。

To review the overload resolution process, first, both are listed as "candidate" functions as they are in the correct scope and have the correct name.

const 中,只有 const 函数进入下一步(称为活力),因此它会自动成为最佳选择。非const成员函数是不可行的,因为你不能将 const 引用转换为非const引用。

In the const case, only the const member function gets to the next step (called "viability"), so it's automatically the best choice. The non-const member function is not viable because you can't convert a const reference into a non-const reference.

在非const的情况下,const和非const版本都是可行的,但是由于下面引用的13.3.3.2第3段的第五条规则,非const版本是更好的。

In the non-const case, both the const and non-const versions are viable, but the non-const one is "better" because of the fifth rule of 13.3.3.2 paragraph 3, quoted below.


标准转换序列S1比
标准转换序列S2更好的转换序列S2如果...

Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if ...

S1和S2是引用绑定,
引用引用的类型是相同的类型,除了顶级cv限定符
和引用初始化的引用类型S2指的是比由S1
初始化的引用所指向的类型更多的
cv限定。

S1 and S2 are reference bindings, and the types to which the references refer are the same type except for top-level cv-qualifiers, and the type to which the reference initialized by S2 refers is more cv-qualified than the type to which the reference initialized by S1 refers.

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

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