gpt4 book ai didi

c++ - 标准在哪里定义了将值绑定(bind)到引用的优先顺序?

转载 作者:行者123 更新时间:2023-11-28 01:43:36 26 4
gpt4 key购买 nike

正如解释的那样here ,不同类别的值根据以下优先顺序绑定(bind)到不同种类的引用:

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 的函数。当然,只考虑可行的重载。

关于c++ - 标准在哪里定义了将值绑定(bind)到引用的优先顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46108455/

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