单个符号是什么意思? [英] What does the single ampersand mean here?

查看:182
本文介绍了单个符号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自此处的回答。

class wrap {
public:
   operator obj() const & { ... }   //Copy from me.
   operator obj() && { ... }  //Move from me.
private:
   obj data_;
};

我知道&& 当对象是右值引用时将调用成员。但是单个符号是什么意思?

I know the && means that the member will be invoked when the object is an rvalue reference. But what does the single ampersand mean? How is it different than without the ampersand?

推荐答案

这意味着当对象是一个左值引用时,成员将被调用。 / p>

It means the member will be invoked when the object is an lvalue reference.


[C + + 11:9.3.1 / 5]:静态成员函数可以用 ref-qualifier (8.3.5)声明;参见13.3.1。

[C++11: 9.3.1/5]: A non-static member function may be declared with a ref-qualifier (8.3.5); see 13.3.1.

[C ++ 11:13.3.1 / 4]:对于非静态成员函数,隐式对象参数的类型是

[C++11: 13.3.1/4]: For non-static member functions, the type of the implicit object parameter is


  • lvue引用 cv c> ref-qualifier 声明的函数的 / em>

  • 对于使用<$ c $声明的函数,引用 cv X c>&&&< / code> ref-qualifier

  • "lvalue reference to cv X" for functions declared without a ref-qualifier or with the & ref-qualifier
  • "rvalue reference to cv X" for functions declared with the && ref-qualifier

其中 X 是函数是成员的类,而 cv 是成员函数声明的 cv-qualification [..]

where X is the class of which the function is a member and cv is the cv-qualification on the member function declaration. [..]

(以及一些我找不到的规则)

(and some more rules that I can't find)

如果没有 ref-qualifier ,函数总是可以被调用,而不管表达式的值类别,重新调用它:

Without a ref-qualifier, the function can always be invoked, regardless of the value category of the expression through which you're invoking it:

struct foo
{
    void bar() {}
    void bar1() & {}
    void bar2() && {}
};

int main()
{
    foo().bar();  // (always fine)
    foo().bar1(); // doesn't compile because bar1() requires an lvalue
    foo().bar2();

    foo f;
    f.bar();      // (always fine)
    f.bar1();
    f.bar2();     // doesn't compile because bar2() requires an rvalue
}

这篇关于单个符号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆