`const&&`绑定到所有prvalues(和xvalues)? [英] Does `const &&` bind to all prvalues (and xvalues)?

查看:185
本文介绍了`const&&`绑定到所有prvalues(和xvalues)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准定义了以下函数已删除:

The C++ standard defines the following functions deleted;

template <class T>
void ref(const T&&) = delete;

template <class T>
void cref(const T&&) = delete;

这是为了帮助确保函数不被滥用,不允许它们绑定到临时值

This is to aid in ensuring that the functions are not misused by disallowing them from binding to temporary values (rvalues).


  • const&&& 绑定到所有rvalues prvalues?

  • const&&& 绑定到所有移动对象(xvalues;基本上从 std :: move 或类似)?

  • Does const && bind to all rvalues, specifically prvalues?
  • Would const && bind to all "moved objects" (xvalues; basically something returned from std::move or similar)?


  • 或者相反,有些情况下,右值(prvalue或xvalue) const&&&


    • 如果是,如何?

    注意:从注释中有一些清楚,这个问题严重影响了经典值,即prvalue值类别。

    Note: some clarity from the comments, this question is heavily swayed to classic rvalues, the prvalue value category.

    推荐答案

    T const&& 可以绑定类型 T const T

    strong> [dcl.init.ref] 段落5:

    From 8.5.3 [dcl.init.ref] paragraph 5:


    5 - 对类型cv1 T1的引用初始化通过类型cv2 T2的表达式如下:[...] $ $ $ b - 否则,[...]引用将是一个右值引用。 [...]
    - 如果初始化表达式
    是一个xvalue,类prvalue,数组prvalue或函数lvalue和cv1 T1是引用兼容的 cv2 T2[...]
    那么引用绑定到初始化器表达式的值[...]

    5 - A reference to type "cv1 T1" is initialized by an expression of type "cv2 T2" as follows: [...]
    — Otherwise, [...] the reference shall be an rvalue reference. [...]
    — If the initializer expression
    — is an xvalue, class prvalue, array prvalue or function lvalue and "cv1 T1" is reference-compatible with "cv2 T2" [...] then the reference is bound to the value of the initializer expression [...]

    如果初始化器表达式是非类型的prvalue,那么将创建一个用于引用绑定的临时副本(ibid)。

    If the initializer expression is a prvalue of non-class type, then a temporary copy is created for reference binding (ibid).

    引用兼容性在8.5 .3p4;它需要一个同类或基类关系和相同或更大的cv限定。

    Reference-compatibility is defined in 8.5.3p4; it requires a same-or-base-class relationship and same-or-greater cv qualification.

    所以对于一个右值绑定到 T const& ;& ,其cv-qualification必须不大于 const

    So for an rvalue to bind to T const&&, its cv-qualification must be no greater than const.

    这篇关于`const&amp;&amp;`绑定到所有prvalues(和xvalues)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆