为什么允许通过const引用而不是普通引用传递R值? [英] Why is it allowed to pass R-Values by const reference but not by normal reference?

查看:94
本文介绍了为什么允许通过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).

这篇关于为什么允许通过const引用而不是普通引用传递R值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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