这个问题是此处显示的相关问题的轻微变体。
在 C++17 中,我有一个局部变量,我想将其设为 const 以证明它在根据 Scott Meyers Effective C++ item 3 建议尽可能使用 const创建后未经修改:
#include <string>
std::string foo()
{
const std::string str = "bar";
return str;
}
int main()
{
std::string txt = foo();
}
编译器能否为 执行(命名)返回值优化txt
,即使由于 const-ness 差异而导致的str
类型与返回类型不同?foo