Wil*_*mKF 2 c++ optimization reference copy-constructor language-lawyer
给出以下C++代码:
struct foo {
// Some definition with copy constructor.
};
const foo &getData();
const foo &alt1(getData());
const foo &alt2 = getData();
Run Code Online (Sandbox Code Playgroud)
合理的编译器会为alt1和生成不同的代码alt2吗?换句话说,是否会alt1导致复制构造函数运行,或者是否允许编译器对其进行优化并直接分配引用?