c++ - C++11 是否保证 return 语句中的局部变量将被 move 而不是复制?

标签 c++ c++11 move-semantics rvalue-reference rvo

#include <vector>

using namespace std;

struct A
{
    A(const vector<int>&) {}
    A(vector<int>&&) {}
};

A f()
{
    vector<int> coll;
    return A{ coll }; // Which constructor of A will be called as per C++11?
}

int main()
{
    f();
}

coll一个xvaluereturn A{ coll }; ?

C++11是否保证A(vector<int>&&)将在 f 时调用返回?

最佳答案

C++11 不允许 coll从中 move 。它只允许 return 中的隐式 move 当你做 return <identifier> 时的陈述, 其中<identifier>是局部变量的名称。任何比这更复杂的表达式都不会隐式 move 。

比这更复杂的表达式不会经历任何形式的省略。

关于c++ - C++11 是否保证 return 语句中的局部变量将被 move 而不是复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42443495/

相关文章:

c++ - 低效率的 std::move()?

c++ - 移出的对象是否应该保留在 "safe"状态?

c++ - 通过组合实现仿函数重载

c++ - 在 C++ 中向 std::vector 添加任意数量的元素的最有效方法是什么?

c++ - auto 关键字的声明点

multithreading - C++ 11线程更新MFC应用程序窗口。需要SendMessage(),PostMessage()吗?

c++ - 尝试编译队列时出现 static_initialization_and_destruction 错误

c++ - 我可以在这里使用模板特化或类型强制吗?

c++ - "This application has requested the Runtime to terminate it in an unusual way."

c++ - 是否允许编译器评估静态断言中的重言式