C++11 binding rules for const &&

末鹿安然 提交于 2019-12-04 07:34:43

'binding' in this context means tying a reference to a particular object.

int a;

int &b = a; // the reference is 'bound' to the object 'a'

void foo(int &c);

foo(a); // the reference parameter is bound to the object 'a'
        // for this particular execution of foo.

http://coliru.stacked-crooked.com/a/5e081b59b5e76e03

So then reading the quote:

Note the asymmetry: while a const lvalue reference can bind to an rvalue,

void foo(int const &);

foo(1); // the const lvalue reference parameter is bound to
        // the rvalue resulting from the expression '1'

http://coliru.stacked-crooked.com/a/12722f2b38c74c75

a const rvalue reference cannot bind to an lvalue.

void foo(int const &&);

int a;

foo(a); // error, the expression 'a' is an lvalue; rvalue
        //references cannot bind to lvalues

http://coliru.stacked-crooked.com/a/ccadc5307135c8e8

In particular, this makes a const lvalue reference able to do everything a const rvalue reference can and more (i.e., bind to lvalues).

void foo(int const &);

foo(1); // const lvalue reference can bind to rvalue

int a;
foo(a); // and lvalue

http://coliru.stacked-crooked.com/a/d5553c99e182c89b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!