[英]C++11 binding rules for const &&
Many people do not know that const
rvalue references are part of the C++11 language. 许多人不知道
const
rvalue引用是C ++ 11语言的一部分。 This blog post discusses them but appears to be mistaken regarding the binding rules. 这篇博客文章讨论了它们,但似乎在绑定规则上有误。 Quoting the blog:
引用博客:
struct s {};
void f ( s&); // #1
void f (const s&); // #2
void f ( s&&); // #3
void f (const s&&); // #4
const s g ();
s x;
const s cx;
f (s ()); // rvalue #3, #4, #2
f (g ()); // const rvalue #4, #2
f (x); // lvalue #1, #2
f (cx); // const lvalue #2
Note the asymmetry: while a const lvalue reference can bind to an rvalue, a const rvalue reference cannot bind to an lvalue.
注意不对称性:当const左值引用可以绑定到右值时,const右值引用不能绑定到左值。 In particular, this makes a const lvalue reference able to do everything a const rvalue reference can and more (ie, bind to lvalues).
特别是,这使得const左值引用能够执行const rvalue引用可以做的更多事情(即绑定到左值)。
The comments on the sample code appear to check out on my installation of GCC 4.9 (with -std=c++14 flag set). 示例代码的注释似乎在我安装GCC 4.9时检查(设置了-std = c ++ 14标志)。 So, contrary to the blog text, is it true that
const &&
should bind to const &
and const &&
and const &
only bind to const &
? 因此,与博客文本相反,
const &&
应该绑定到const &
和const &&
和const &
只绑定到const &
? If not what is the actual rule? 如果不是实际规则是什么?
Here is a demo that appears to show const &&
binding to const&
in GCC 4.9: http://coliru.stacked-crooked.com/a/794bbb911d00596e 这是一个演示,显示
const &&
绑定到const&
in GCC 4.9: http : //coliru.stacked-crooked.com/a/794bbb911d00596e
'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 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,
注意不对称性:虽然const左值引用可以绑定到右值,
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 http://coliru.stacked-crooked.com/a/12722f2b38c74c75
a const rvalue reference cannot bind to an lvalue.
const rvalue引用不能绑定到左值。
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 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 (ie, bind to lvalues).
特别是,这使得const左值引用能够执行const rvalue引用可以做的更多事情(即绑定到左值)。
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 http://coliru.stacked-crooked.com/a/d5553c99e182c89b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.