使用整数指针将左值转换为右值
Lvalue to rvalue conversion with integer pointer
如果我 implicit conversions 没看错:
Lvalue to rvalue conversion
A glvalue of any non-function, non-array type T can be implicitly converted to a prvalue of the same type. [..]
Unless encountered in unevaluated context (in an operand of sizeof, typeid, noexcept, or decltype), this conversion effectively copy-constructs a temporary object of type T using the original glvalue as the constructor argument, and that temporary object is returned as a prvalue.
那为什么不行呢?
int* iptr = nullptr;
int*&& irr = iptr; // Cannot bind lvalue to rvalue reference
类型 int*
应该已经通过临时隐式转换为相同类型的纯右值 - 因此可以毫无问题地绑定到右值引用。
标准明确指出这是错误的;不会(不应该)考虑左值到右值的转换。
if the reference is an rvalue reference, the initializer expression shall not be an lvalue.
[ Example:
double d2 = 1.0;
double&& rrd2 = d2; // error: initializer is lvalue of related type
如果我 implicit conversions 没看错:
Lvalue to rvalue conversion
A glvalue of any non-function, non-array type T can be implicitly converted to a prvalue of the same type. [..]
Unless encountered in unevaluated context (in an operand of sizeof, typeid, noexcept, or decltype), this conversion effectively copy-constructs a temporary object of type T using the original glvalue as the constructor argument, and that temporary object is returned as a prvalue.
那为什么不行呢?
int* iptr = nullptr;
int*&& irr = iptr; // Cannot bind lvalue to rvalue reference
类型 int*
应该已经通过临时隐式转换为相同类型的纯右值 - 因此可以毫无问题地绑定到右值引用。
标准明确指出这是错误的;不会(不应该)考虑左值到右值的转换。
if the reference is an rvalue reference, the initializer expression shall not be an lvalue.
[ Example:
double d2 = 1.0; double&& rrd2 = d2; // error: initializer is lvalue of related type