本文介绍了`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:

    如果初始化器表达式是非类型的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)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:12