简体   繁体   English

const 引用如何绑定到右值

[英]How const reference bound to rvalue

I have seen quite a few FAQ posts about binding const reference to rvalue.我看过很多关于将 const 引用绑定到右值的常见问题解答帖子。 Some said it's because system generates a temporary object for reference to bind to, and some said it's just an abstract system rule, it may not really doing this.有人说是因为系统生成了一个临时的 object 供参考绑定,也有人说这只是一个抽象的系统规则,可能并没有真正做到这一点。 So which side should I follow?那么我应该跟随哪一方呢? Could someone kindly explain the true mechanism about binding const reference to a rvalue or just simply find me a relevant and confirm accurate link.有人可以解释一下将 const 引用绑定到右值的真正机制,或者只是简单地给我找一个相关的并确认准确的链接。 Many thanks in advance!!提前谢谢了!!

Both are correct. 两者都是正确的。 You have to understand that C++ is an abstraction. 你必须明白C ++是一种抽象。 The code that your computer executes looks nothing like it. 您的计算机执行的代码看起来不像它。

When you bind a temporary to a const reference, you could imagine various C++ mechanisms to represent how your compiler's making that work. 当您将临时绑定到const引用时,您可以想象各种C ++机制来表示编译器如何使其工作。 Or you could just accept that it does make that work, and that your compiler is a black box whose internals you do not need to reason about. 或者你可以接受它确实有效,并且你的编译器是一个黑盒子,你不需要推理它的内部。 The two are indistinguishable , for all intents and purposes. 无论出于何种意图和目的,这两者都难以区分 That's the whole point of constructing models. 这就是构建模型的重点。

If you really, really, really want to know how your specific compiler is implementing a specific piece of code under certain circumstances, you will have to become an expert on that compiler's source code. 如果您确实非常非常想知道特定编译器在某些情况下如何实现特定代码,那么您必须成为该编译器源代码的专家。 This is an incredibly advanced topic, and typically there are only a handful of people in the world who understand what a compiler is doing inside. 这是一个令人难以置信的高级主题,通常世界上只有少数人了解编译器在做什么。 It's generally not worth the time and investment required to become one of them. 通常不值得成为其中之一所需的时间和投资。 1 1

1 Although I'm sure they'd be extremely grateful for the extra help! 1虽然我确信他们会非常感谢你的额外帮助! They'll eventually need to "pass the torch", after all. 毕竟,他们最终还是需要“通过火炬传递”。

Herb Sutter writted a nice article about const reference to rvalue, here is the link. Herb Sutter 写了一篇关于 const reference to rvalue 的好文章, 这里是链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM