简体   繁体   中英

How const reference bound to rvalue

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.

Herb Sutter writted a nice article about const reference to rvalue, here is the link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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