Const引用临时对象 - 为什么?
大家好,
虽然以下定义是合法的:
const int& i = 5;
....并且我所引用的临时变量的生命周期是
与我自己相同,为什么有人想要这样做而不是简单的
const int i = 5;
....?
我可以看到如何将const引用绑定到临时对象
(例如将rvalue传递给期望const引用的函数时),
但上面的用法让我感到困惑。
-dr
Hi all,
Although the following definition is legal:
const int& i = 5;
....and that the lifetime of the temporary variable to which i refers is
identical to i itself, why would anyone want to do this instead of a simple
const int i = 5;
....?
I can see how binding a const reference to a temporary object is necessary
(such as when passing an rvalue to a function expecting a const reference),
but the above usage perplexes me.
-dr
Dave Rahardja写道:
Dave Rahardja wrote:
虽然以下定义是合法的:
const int& i = 5;
...我所指的临时变量的生命周期与我自己相同,为什么有人想要这样做而不是
一个简单的
const int i = 5;
......?
你应该问那些做过/做过的人。
我可以看到如何将const引用绑定到临时对象是必要的(例如当将右值传递给期望
const引用的函数时,但上面的用法让我感到困惑。
Although the following definition is legal:
const int& i = 5;
...and that the lifetime of the temporary variable to which i refers
is identical to i itself, why would anyone want to do this instead of
a simple
const int i = 5;
...?
You should ask somebody who did/does that.
I can see how binding a const reference to a temporary object is
necessary (such as when passing an rvalue to a function expecting a
const reference), but the above usage perplexes me.
该代码本身没有任何困惑。在生产代码库中,它会非常困惑。在生产代码中我从未见过
。
V
There is nothing perplexing in that code by itself. It _would_ be
seriously perplexing in a production codebase. I have never seen
anything like that in production code.
V
>我在生产代码中从未见过这样的东西。
> I have never seen anything like that in production code.
V
戴夫 -
听听Victor,我在生产中都没有看过这个,而不是
Victor'的代码。我知道维克多已经有一段时间了(Vitya就是这样吗?b $ 25美元或者30年总共是我们相识的时间和
最好的朋友?)。
Dave -
Do listen to Victor, I neither had seen this in production not
Victor''s code. And I know Victor for quite some time now (Vitya is it
25 or 30 years in total is the duration of our acquaintance and
bestfriendship?).
> Dave -
> Dave -
请听听Victor,我没有在制作中看过这个而不是Victor的代码。我知道维克多已经有一段时间了(Vitya是不是总共25或30年是我们熟人的时间和最好的朋友?)。
Do listen to Victor, I neither had seen this in production not
Victor''s code. And I know Victor for quite some time now (Vitya is it
25 or 30 years in total is the duration of our acquaintance and
bestfriendship?).
这是很好的建议。如果任何具有c ++专业知识水平的人在IRC,MIME和POP3协议方面有经验,我很乐意接受工作
申请在美国的职位。申请人必须具备管理程序员团队的技能。
That is great advice. If anyone with your level of expertise in c++ has
experience in IRC, MIME and POP3 protocols I am happy to accept Job
Application for position in the US. Applicants must have skills of a
management of a team of the programmers.