I had asked previously a question on stackoverflow (if you are interested here’s the link: Passing by reference "advanced" concept? )
Interestingly, one of the answers intrigued me and I felt it deserves a separate question.
const int& x = 40;
If 40 happens to be a value in the CPU cache (rvalue). Then would you, by writing that line, just reserved cache memory to hold the number 40 for the lifetime of your process? And isn’t that a bad thing?
Thank you
The literal
40almost certainly lives in some read-only memory, possibly in the assembler (for small values there are typically instructions which can set a register or address; for bigger values it would be living somewhere as constant). It doesn’t live “in the cache”. When you create aconstreference to it, a temporary is constructed wherever the compiler sees fit to keep temporaries (probably on the stack). Whether this lives in any cache is up to the system.If the address of this temporary is never taken, it may actually not even be created: All rules in the C++ standard are governed by the “as if”-rule. As a result, the reference and the literal would be identical. If the address of the
constreference is ever taken, the compiler needs to decide where to put the object and you may, indeed, see a small performance impact.