gpt4 book ai didi

c++ - 使用 L-value-ref 进行部分排序

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:16:44 24 4
gpt4 key购买 nike

为什么这是模棱两可的?

template<class T> void g(T)  {}   // 1
template<class T> void g(T&) {} // 2

int main() {
int q;
g(q);
}

我知道这是部分排序上下文。我的想法可能是错误的:#2 中的任何 T& 都可以放在#1 中,但#1 中的任何 T 都不能放在#2 中。所以部分排序应该有效。

最佳答案

好的。我认为这就是您要找的。没有深入研究参数与参数类型比较的两次应用,标准中的以下内容让我大吃一惊:

C++11 §14.8.2.4p5

Before the partial ordering is done, certain transformations are performed on the types used for partial ordering:

  • If P is a reference type, P is replaced by the type referred to.
  • If A is a reference type, A is replaced by the type referred to.

C++11 §14.8.2.4p6 继续讨论当两者 都是引用类型时会发生什么,但这在这里不适用(尽管也是一个有趣的读物)。在你的情况下,只有一个,所以它被剥离了。从那里:

C++11 §14.8.2.4p7

Remove any top-level cv-qualifiers:

  • If P is a cv-qualified type, P is replaced by the cv-unqualified version of P.
  • If A is a cv-qualified type, A is replaced by the cv-unqualified version of A.

现在两者完全相等,因此你有歧义,我相信这是从 C++11 §14.8.2.4p10 巩固的。 C++11 §14.8.2.4p9 的文本涵盖了这两种引用类型,而这里又不是这种情况:

C++11 §14.8.2.4p10

If for each type being considered a given template is at least as specialized for all types and more specialized for some set of types and the other template is not more specialized for any types or is not at least as specialized for any types, then the given template is more specialized than the other template. Otherwise, neither template is more specialized than the other.

但阅读本节中的标准对我来说就像破译希腊语,所以我可能方式偏离了基础。 (对希腊人没有冒犯 =P)。

然而,它确实让我想到了“const T&T,给定相同的调用条件 g(q),如果我刚刚阅读的所有内容都按书面执行,那么也应该是模棱两可的。”果然,我试了一下,同样的歧义被标记出来了。

关于c++ - 使用 L-value-ref 进行部分排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14453829/

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