本文介绍了对象,右值引用,const引用之间的过载解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定所有三个函数,此调用是不明确的。

Given all three functions, this call is ambiguous.

int f( int );
int f( int && );
int f( int const & );

int q = f( 3 );

删除 f(int)和GCC相对于左值引用偏好右值引用。但是相反,删除引用过载会导致与 f(int)不清楚。

Removing f( int ) causes both Clang and GCC to prefer the rvalue reference over the lvalue reference. But instead removing either reference overload results in ambiguity with f( int ).

过载解决方法通常的严格部分排序,但 int 似乎相当于两个彼此不相等的东西。这里的规则是什么?我似乎记得有关这个的缺陷报告。

Overload resolution is usually done in terms of a strict partial ordering, but int seems to be equivalent to two things which are not equivalent to each other. What are the rules here? I seem to recall a defect report about this.

有可能是 int&& 在未来的标准中 int ?引用必须绑定到初始化程序,而对象类型不受此约束。因此, T T&& 之间的重载可以有效地表示所有权,否则复制。 (这类似于纯值传递,但是节省了移动的开销)。由于这些编译器当前工作,这必须通过重载 T const& T&&& ,并明确复制。

Is there any chance int && may be preferred over int in a future standard? The reference must bind to an initializer, whereas the object type is not so constrained. So overloading between T and T && could effectively mean "use the existing object if I've been given ownership, otherwise make a copy." (This is similar to pure pass-by-value, but saves the overhead of moving.) As these compilers currently work, this must be done by overloading T const & and T &&, and explicitly copying. But I'm not even sure even that is strictly standard.

推荐答案

由于只有一个参数,规则是该参数的三个可行参数初始化之一必须更好的匹配两者。当比较两个初始化时,任一个比另一个更好,或者两者都不更好(它们是不可区分的)。

As there is only one parameter, the rule is that one of the three viable parameter initializations of that parameter must be a better match than both the other two. When two initializations are compared, either one is better than the other, or neither is better (they are indistinguishable).

没有关于直接引用绑定的特殊规则, (在所有三个比较中)。

Without special rules about direct reference binding, all three initializations mentioned would be indistinguishable (in all three comparisons).

关于直接引用绑定的特殊规则make int&&< / code >优于 const int& ,但都不比 int 更好或更差。因此没有最佳匹配:

The special rules about direct reference binding make int&& better than const int&, but neither is better or worse than int. Therefore there is no best match:

S1    S2
int   int&&         indistinguishable
int   const int&    indistinguishable
int&& const int&    S1 better

int&&& const int& 因为13.3.3.2:

int&& is better than const int& because of 13.3.3.2:

但是,当其中一个初始化不是引用绑定时,此规则不适用。

But this rule does not apply when one of the initializations is not a reference binding.

引用绑定比非引用绑定更好的匹配。为什么不将您的想法发布到。 SO不是主观讨论/意见的最佳选择。

You propose to make a reference binding a better match than a non-reference binding. Why not post your idea to isocpp future proposals. SO is not the best for subjective discussion / opinion.

这篇关于对象,右值引用,const引用之间的过载解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 15:48
查看更多