请考虑这三个函数。
std::string get_a_string()
{
return "hello";
}
std::string get_a_string1()
{
return std::string("hello");
}
std::string get_a_string2()
{
std::string str("hello");
return str;
}
- 在这三种情况下,RVO是否会被应用?
- 在上面的代码中返回一个临时对象是否可行?我认为可以,因为我是通过值返回它,而不是返回任何引用。
有什么想法吗?
/Od
应该禁用 NRVO,但我不确定 RVO。 - Kirill V. Lyadvinsky};myClass foo() { myClass bar; return bar;
}int main() { myClass baz = foo(); } - Alexandre Bell