I have seen quite a few FAQ posts about binding const reference to rvalue. 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. 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. Many thanks in advance!!
Both are correct. You have to understand that C++ is an abstraction. 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. 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 Although I'm sure they'd be extremely grateful for the extra help! They'll eventually need to "pass the torch", after all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With