函数结尾的&&是什么意思

时间:2019-08-09 09:29:58

标签: c++

阅读此page

时我很困惑
struct S
{
    virtual int f(char) const, g(int) &&; // declares two non-static member functions
    virtual int f(char), x; // compile-time error: virtual (in decl-specifier-seq)
                            // is only allowed in declarations of non-static
                            // member functions
};

什么是

virtual int g(int) &&;

关于?

1 个答案:

答案 0 :(得分:1)

&&位于函数末尾表示this具有r值值类别时选择该函数。

请参见member functions

  

可以在没有ref限定符的情况下声明一个非静态成员函数,而没有lvalue ref限定符(参数列表后面的标记&)或rvalue ref限定符(参数列表后面的标记&&)。在重载解析期间,对X类的非静态cv限定成员函数的处理如下:

     

没有ref限定词:隐式对象参数具有对cv限定的X的左值类型引用,并且还可以绑定右值隐式对象参数

     

左值ref限定词:隐式对象参数具有对cv限定X的左值引用

     

rvalue ref-qualifier:隐式对象参数具有对cv限定的X的rvalue类型引用