为什么允许通过const引用而不是普通引用传递R值?
标题说明为什么允许通过常量引用而不是常规引用传递R值(文字)
as the title says why is it allowed to pass R-Values(literals) by constant reference but not normal reference
void display(const int& a)
{
cout << a ;
}
如果被称为display(5)
将起作用,但是如果没有const
它将不起作用******我的意思是const
引用如何保持指向R值(匿名变量)的方式*** ***
will work if called display(5)
but without the const
it won't work ****** I mean how can a const
reference keep pointing to an R-Value (anonymous variable) ******
最后一个问题:
常量引用如何保持指向R值(匿名变量)
how can a const reference keep pointing to an R-Value (anonymous variable)
这是答案. C ++语言表示,本地const引用可以将临时值的生存期延长到包含作用域的末尾,但可以节省复制构造的成本(即,如果您要使用本地变量代替).
Here is the answer. The C++ language says that a local const reference prolongs the lifetime of temporary values until the end of the containing scope, but saving you the cost of a copy-construction (i.e. if you were to use an local variable instead).