右值绑定到非const引用

iamnobody 2014-01-13 06:28:00
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引用是什么情况?
求举例说明.
...全文
208 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
vipcxj 2014-01-13
  • 打赏
  • 举报
回复
我擦,杯具,已经结贴了~
vipcxj 2014-01-13
  • 打赏
  • 举报
回复
引用 4 楼 vipcxj 的回复:

struct S {
    void f() const & { std::cout << typeid(this).name() << "-const-lv" << std::endl; }
    void f() & { std::cout << typeid(this).name() << "-lv" << std::endl; }
    void f() &&{ std::cout << typeid(this).name() << "-rv" << std::endl; }
    void f() const &&{ std::cout << typeid(this).name() << "-const-rv" << std::endl; }
};

const S fun()
{
    const S s;
    return s;
}
 
int main(){
    S s;
    s.f();            // prints "lvalue"
    std::move(s).f(); // prints "rvalue"
    S().f();          // prints "rvalue"
    const S cs;
    cs.f();
    fun().f(); 
/*
注意这里,注释了这个才能编译过去,之所以编译不过去,是因为这条语句让编译器难以绝定改重载哪个,可能是void f() const &,又可能是void f() const &&。换句话说常右值引用对编译器来说和常左值引用没区别
*/
}
如果注释了fun().f();得到的结果是 P1S-lv P1S-rv P1S-rv PK1S-const-lv
其实上面的例子中fun()返回的显然是右值,而且还是常的,照理应该重载void f() const && 但事实上它就是编译不过去,编译器认为void f() const && 和 void f() const &是一样滴。 换句话说常的右值木有啥意义,右值只能绑定到非常版本的成员函数,个人理解,可能不对~
vipcxj 2014-01-13
  • 打赏
  • 举报
回复

struct S {
    void f() const & { std::cout << typeid(this).name() << "-const-lv" << std::endl; }
    void f() & { std::cout << typeid(this).name() << "-lv" << std::endl; }
    void f() &&{ std::cout << typeid(this).name() << "-rv" << std::endl; }
    void f() const &&{ std::cout << typeid(this).name() << "-const-rv" << std::endl; }
};

const S fun()
{
    const S s;
    return s;
}
 
int main(){
    S s;
    s.f();            // prints "lvalue"
    std::move(s).f(); // prints "rvalue"
    S().f();          // prints "rvalue"
    const S cs;
    cs.f();
    fun().f(); 
/*
注意这里,注释了这个才能编译过去,之所以编译不过去,是因为这条语句让编译器难以绝定改重载哪个,可能是void f() const &,又可能是void f() const &&。换句话说常右值引用对编译器来说和常左值引用没区别
*/
}
如果注释了fun().f();得到的结果是 P1S-lv P1S-rv P1S-rv PK1S-const-lv
iamnobody 2014-01-13
  • 打赏
  • 举报
回复
引用 2 楼 supermegaboy 的回复:
[quote=引用 楼主 mingliang1212 的回复:] 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引用是什么情况? 求举例说明.
请注意看前面一段第一句话的说明: For non-static member functions, the type of the implicit object parameter is “reference to cv X” where X is the class of which the function is a member and cv is the cv-qualification on the member function declaration. [Example: for a const member function of class X, the extra parameter is assumed to have type “reference to const X”. ] 隐含形参是否cv-qualified是依据成员函数的const而定的,当一个临时对象调用非const的成员函数时,即使此时隐含形参的类型是reference to X,这个临时对象也必须保持到实参身上的转换完成为止(否则无法保证不发生非确定性结果),例如:

struct A
{
    void Do() const
    {
        std::cout<<"const"<<std::endl;
    }
    void Do()
    {
        std::cout<<"noconst"<<std::endl;
    }
};

struct B : A
{

};

int main( void )
{
    B().Do();
    return 0;
}
[/quote] 又看多了几遍,好像看懂了,之前在纠结的是这句:as long as in all other respects the temporary can be converted to the type of the implicit object parameter 我以为all other respects 会包括这种情况: A &r = A(); (显然是不行的),,所以我不知道它的all other respects 是指什么..
飞天御剑流 2014-01-13
  • 打赏
  • 举报
回复
引用 楼主 mingliang1212 的回复:
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引用是什么情况? 求举例说明.
请注意看前面一段第一句话的说明: For non-static member functions, the type of the implicit object parameter is “reference to cv X” where X is the class of which the function is a member and cv is the cv-qualification on the member function declaration. [Example: for a const member function of class X, the extra parameter is assumed to have type “reference to const X”. ] 隐含形参是否cv-qualified是依据成员函数的const而定的,当一个临时对象调用非const的成员函数时,即使此时隐含形参的类型是reference to X,这个临时对象也必须保持到实参身上的转换完成为止(否则无法保证不发生非确定性结果),例如:

struct A
{
    void Do() const
    {
        std::cout<<"const"<<std::endl;
    }
    void Do()
    {
        std::cout<<"noconst"<<std::endl;
    }
};

struct B : A
{

};

int main( void )
{
    B().Do();
    return 0;
}
偏爱风流 2014-01-13
  • 打赏
  • 举报
回复
见过const引用的,但没见过非const引用。 不知道这样算不算非const引用 int &&a = (1+2); 某书见过const的右值引用 const int& a = (1+2);

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧