Given the following C++ code:
struct foo {
// Some definition with copy constructor.
};
const foo &getData();
const foo &alt1(getData());
const foo &alt2 = getData();
Will a reasonable compiler produce different code for alt1 and alt2? In other words, will alt1 cause the copy constructor to be run, or is the compiler allowed to optimize that away and assign the reference directly?
In both cases, you initialize a reference (not an object), so no copy-constructor will be run.
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