标准在哪里定义了将值绑定到引用的优先顺序?
Where does the standard define the order of preference for binding of values to references?
正如所解释的那样here,不同类别的值根据以下优先顺序绑定到不同种类的引用:
struct s {};
void f ( s&); // #1
void f (const s&); // #2
void f ( s&&); // #3
void f (const s&&); // #4
const s g ();
s x;
const s cx;
f (s ()); // rvalue #3, #4, #2
f (g ()); // const rvalue #4, #2
f (x); // lvalue #1, #2
f (cx); // const lvalue #2
标准中哪里描述了这种优先顺序?
[over.ics.rank]/3
- (3.1) Standard conversion sequence S1
is a better conversion sequence than standard conversion sequence S2
if
...
(3.1.3) — S1
and S2
are reference bindings (8.5.3) and neither refers to an implicit object parameter of a non-static member function declared without a ref-qualifier, and S1
binds an rvalue reference to an rvalue and S2
binds an lvalue reference.
...
(3.1.6) — S1
and S2
are reference bindings (8.5.3), 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 引用的函数优于采用 const 的函数。当然,只考虑可行的重载。
正如所解释的那样here,不同类别的值根据以下优先顺序绑定到不同种类的引用:
struct s {};
void f ( s&); // #1
void f (const s&); // #2
void f ( s&&); // #3
void f (const s&&); // #4
const s g ();
s x;
const s cx;
f (s ()); // rvalue #3, #4, #2
f (g ()); // const rvalue #4, #2
f (x); // lvalue #1, #2
f (cx); // const lvalue #2
标准中哪里描述了这种优先顺序?
[over.ics.rank]/3 - (3.1) Standard conversion sequence
S1
is a better conversion sequence than standard conversion sequenceS2
if...
(3.1.3) —
S1
andS2
are reference bindings (8.5.3) and neither refers to an implicit object parameter of a non-static member function declared without a ref-qualifier, andS1
binds an rvalue reference to an rvalue andS2
binds an lvalue reference....
(3.1.6) —
S1
andS2
are reference bindings (8.5.3), 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 byS2
refers is more cv-qualified than the type to which the reference initialized byS1
refers.
根据这些规则,采用右值引用的函数优于采用左值引用的函数,然后采用非 const 引用的函数优于采用 const 的函数。当然,只考虑可行的重载。