简体   繁体   English

const成员函数的重载决策C ++

[英]Overload resolution C++ for const member functions

Assume you have a class T with two member functions 假设您有一个具有两个成员函数的类T.

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

It is my understanding that when called for a constant T, we resolve to (1); 我的理解是,当要求一个常数T时,我们决定(1); and for a non-constant T, we resolve to (2). 对于非常数T,我们决定(2)。

  1. is that correct? 那是对的吗?
  2. which rule is invoked in this resolution? 在这个决议中调用哪个规则? (reference to standard great, but a helpful brief summary appreciated) (参考标准很棒,但有用的简要总结表示赞赏)

Notes: 笔记:

  1. I tried to google for it, but old hits I got on SO were cases for other overload resolutions involving const. 我试图谷歌为它,但我得到的旧命中是其他重载决议涉及const的情况。 However, link to an old SO actually explaining the above obviously great. 然而,链接到一个老SO实际上解释上面显然很棒。

  2. This came up when re-reading Stroustrup's "The C++ programming language", 2nd edition ("Special Edition"), String/Cref example in section 11.12, p. 当重新阅读Stroustrup的“The C ++编程语言”,第2版(“特别版”),第11.12节中的String / Cref示例时,就出现了这一点。 296. As Stroustrup is so precise, the answer might be in previous sections, but I fail to see where. 296.由于Stroustrup是如此精确,答案可能在前几节,但我没有看到在哪里。 Reference to sections in Stroustrup very welcome too (2nd edition best as this is the one I have). 参考Stroustrup中的部分也非常受欢迎(第2版最好,因为这是我所拥有的)。 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. 第10.2.6节将const成员引入为“不改变对象值的那些”,这暗示了答案,但并没有将我作为明确的解决方案指令。

In N3242 (the standard draft I have on hand), 13.3.1 paragraph 4 says 在N3242(我手头的标准草案)中,13.3.1第4段说

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 对于没有引用限定符或使用&ref-qualifier声明的[非静态成员]函数,隐式对象参数的类型是“对cv X的左值引用”

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 (ie const or non-const). 这意味着首先出现的隐式对象参数的类型是“对cv X左值引用”,其中X是类, cv是成员变量的cv限定(即const或非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. 要查看重载解析过程,首先,两者都列为“候选”函数,因为它们位于正确的范围内并具有正确的名称。

In the const case, only the const member function gets to the next step (called "viability"), so it's automatically the best choice. const情况下,只有const成员函数进入下一步(称为“生存能力”),因此它自动成为最佳选择。 The non-const member function is not viable because you can't convert a const reference into a non-const reference. 非const成员函数不可行,因为您无法将const引用转换为非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. 在非常量情况下,const和非const版本都是可行的,但非常量版本是“更好的”,因为下面引用的第13.3.3.2段第3条规则。

Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if ... 标准转换序列S1是比标准转换序列S2更好的转换序列,如果......

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. S1和S2是引用绑定,引用引用的类型除了顶级cv限定符之外是相同的类型,并且S2引用的引用所引用的类型比cv限定的类型更具cv限定类型。参考由S1初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM