简体   繁体   English

为什么除了在 Uref 实施例中对右值的引用之外,还允许 std::move 接受对右值的引用?

[英]Why was it nessesury to allow std::move accept reference to rvalue besides reference to rvalue in Uref embodiment for both?

Considering that rvalue-ness and lvalue-ness are not a features of objects but of expressions.考虑到右值和左值不是对象的特征而是表达式的特征。

Why isn't std::move implemented only for lvalue reference argument, but for universal one?为什么std::move不是仅针对左值引用参数实现的,而是针对通用参数实现的? Whereas it is useful only for retrieving rvalue reference from an lvalue?而它仅对从左值检索右值引用有用? When rvalue is rvalue already and it does not need std::move functionality.当 rvalue 已经是 rvalue 并且它不需要std::move功能。

template <typename T>
typename std::remove_reference_t<T>&& move(T&& x)
{
  return static_cast<std::remove_reference_t<T>&&>(x);
}

Why isn't std::move implemented only for lvalue reference argument, but for universal one?为什么 std::move 不是仅针对左值引用参数实现的,而是针对通用参数实现的?

Because lvalue references to non-const cannot be bound to xvalues.因为对非常量的左值引用不能绑定到 xvalue。

When rvalue is rvalue already and it does not need std::move functionality.当 rvalue 已经是 rvalue 并且它不需要 std::move 功能。

We want std::move(some_expression) to work whether some_expression is an rvalue expression or lvalue expression.无论some_expression是右值表达式还是左值表达式,我们都希望std::move(some_expression)起作用。 That is particularly important in templates where that detail can depend on template arguments and we don't want to write separate specialisations for each.这在模板中尤其重要,因为模板的细节可能取决于模板 arguments,我们不想为每个模板编写单独的特化。

Basically same reason as why these are allowed:与为什么允许这些基本相同的原因:

using T = const U;
const T var;

using S = T&;
S& ref = var;

In this case, we want to be able to declare const T var , and S& whether the types are already const/reference or not.在这种情况下,我们希望能够声明const T varS&类型是否已经是 const/reference。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM