右值会悄无声息地衰减吗?

Do rvalues decay silently?

std::vector foo( std::vector && rval )
{
    return std::move( rval );
}

如果一个函数需要一个右值引用但得到了其他东西——例如const 引用或临时引用或与 std::move(vec) 不同的任何内容,它会默默地制作副本而不是抛出错误甚至警告吗?

如果期望右值引用的函数获得常量引用,则编译器会报错。如果期望右值引用的函数获得 const,则编译器再次给出错误。

如果您还定义了一个函数,该函数期望发送带有 std::move 的 const 右值 ref 和 const ref (std::move(const &)) 那么它就可以工作。但是,定义一个期望 const 右值 ref 的函数是不合理的,因为这意味着从 const 只读中窃取。

将临时值发送到期望右值引用的函数是可能的并且是合理的。

自己试试看:

#include <iostream>

struct S {
    S() { }
    S(const S& other) { std::cout << "copy ctor" << std::endl; }
    S(S&& other) { std::cout << "move ctor" << std::endl; }
}; 

int foo( S && rval )
{
    return 1;
}

int main()
{
    S s1;
    foo (s1);
}

抄S的不无声。那么,当您尝试编译它时会发生什么?

这个happens(神马):

<source>: In function 'int main()':
<source>:17:10: error: cannot bind rvalue reference of type 'S&&' to lvalue of type 'S'
   17 |     foo (s1);
      |          ^~
<source>:9:15: note:   initializing argument 1 of 'int foo(S&&)'
    9 | int foo( S && rval )
      |          ~~~~~^~~~

所以,你的问题的答案是“否”。