右值绑定到非const引用
c++ 2003
13.3.1.5
During overload resolution, the implied object argument is indistinguishable from other arguments. The
implicit object parameter, however, retains its identity since conversions on the corresponding argument
shall obey these additional rules:
— no temporary object can be introduced to hold the argument for the implicit object parameter;
— no user-defined conversions can be applied to achieve a type match with it; and
— even if the implicit object parameter is not const-qualified, an rvalue temporary can be bound to the
parameter as long as in all other respects the temporary can be converted to the type of the implicit
object parameter.
最后一点说的是右值可以绑定到非const引用是什么情况?
求举例说明.
13.3.1.5
During overload resolution, the implied object argument is indistinguishable from other arguments. The
implicit object parameter, however, retains its identity since conversions on the corresponding argument
shall obey these additional rules:
— no temporary object can be introduced to hold the argument for the implicit object parameter;
— no user-defined conversions can be applied to achieve a type match with it; and
— even if the implicit object parameter is not const-qualified, an rvalue temporary can be bound to the
parameter as long as in all other respects the temporary can be converted to the type of the implicit
object parameter.
最后一点说的是右值可以绑定到非const引用是什么情况?
求举例说明.
...全文
相关推荐
浅谈C++左值引用和右值引用
相反,返回非引用类型的函数、算术运算、关系运算、位运算以及后置递增递减运算符返回的是右值,它们不能被左值引用直接绑定,但可以被const左值引用或右值引用绑定。
浅析C++11中的右值引用、转移语义和完美转发
右值引用有特殊的绑定规则:常量左值引用可以绑定到任何左值,非常量右值引用只能绑定到非常量右值,常量右值引用则可以绑定到任何右值。
非常量引用的初始值必须为左值的问题
非常量引用与常量引用##### 3.1 非常量引用非常量引用只能绑定到非常量左值,即持久存在的非常量对象。这是因为非常量引用可以用来修改其绑定的对象,因此不允许绑定到常量左值或任何类型的右值。
深入理解c++右值引用1
而常量右值引用则更加通用,它可以绑定到任何类型的右值,包括非常量右值和常量右值。
c++ const引用与非const引用介绍
然而,const引用可以绑定到不同但相关的类型的对象,或者绑定到右值。这是因为const引用提供了安全的间接访问,即使对象的类型与引用的类型不完全匹配,也不会导致意外的修改。


