要返回std :: move(x)还是不?

时间:2013-04-12 21:33:29

标签: c++ move-semantics

std::vector<double> foo ()
{
    std::vector<double> t;
    ...

    return t;
}

std::vector<double> foo ()
{
    std::vector<double> t;
    ...

    return std::move (t);
}

相当于?

更确切地说,return x始终等同于return std::move (x)

1 个答案:

答案 0 :(得分:14)

它们不等同,您应该始终使用return t;

较长的版本是,当且仅当return语句符合返回值优化条件时,则返回者绑定到右值引用(或者通俗地说,“move是隐式的”)。

然而,通过拼写return std::move(t);,您实际上会禁止返回值优化!