Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How const reference bound to rvalue

Tags:

c++

reference

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!!

like image 346
Des1gnWizard Avatar asked Sep 03 '25 04:09

Des1gnWizard


1 Answers

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.

like image 86
Lightness Races in Orbit Avatar answered Sep 04 '25 18:09

Lightness Races in Orbit