【问题标题】:To return std::move (x) or not?是否返回 std::move (x)?
【发布时间】:2013-04-05 13:29:08
【问题描述】:

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)

【问题讨论】:

    标签: c++ move-semantics


    【解决方案1】:

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

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

    但是,通过拼写 return std::move(t);,实际上会抑制返回值优化!

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 2022-10-27
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多