引用如何绑定到纯右值?

How references can bind to prvalues?

cppreference says that:当引用绑定纯右值时创建一个临时对象。它们是指 const 左值引用和右值引用吗?:

Temporary objects are created when a prvalue is materialized so that it can be used as a glvalue, which occurs (since C++17) in the following situations:

  • binding a reference to a prvalue

如果他们的意思是,绑定到相同类型的纯右值的右值引用和 const 左值引用是否会创建一个临时对象?我的意思是,这是否正在发生:

const int &x = 10; // does this creates temporary?

int &&x2 = 10; // does this creates temporary?

唯一允许绑定到对象右值(包括纯右值)的引用是右值引用和constvolatile左值引用。当这种绑定发生在纯右值上时,一个临时对象被具体化。因此,在 OP 的两个示例中都会发生临时物化:

const int &x = 10;
int &&x2 = 10;

x 超出范围时,第一个临时文件(值为 10)将被销毁。当 x2 超出范围时,第二个临时文件(也具有值 10,尽管可以使用 x2 修改其值)将被销毁。