本文介绍了返回的本地变量自动xvalues的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我对此发表的评论继续:

Following on from a comment I made on this:


std :: move 以下代码,以确保返回的值是xvalue?

passing std::vector to constructor and move semanticsIs the std::move necessary in the following code, to ensure that the returned value is a xvalue?

std::vector<string> buildVector()
{
  std::vector<string> local;

  // .... build a vector

  return std::move(local);
}

这是我的理解,这是必需的。我经常看到这个用于从函数返回 std :: unique_ptr ,但GManNickG做了以下评论:

It is my understanding that this is required. I have often seen this used when returning a std::unique_ptr from a function, however GManNickG made the following comment:

我的理解是,在返回语句中,所有局部变量都是自动xvalues(过期值)并被移动,但我不确定这是否仅适用于返回的对象本身。所以OP应该去把它放在那里,直到我更有信心,它不应该是。 :)

任何人都可以澄清是否需要 std :: move

Can anyone clarify if the std::move is necessary?

行为编译器是否依赖?

Is the behaviour compiler dependent?

推荐答案

您可以保证 local 在这个情况下。通常编译器会执行返回值优化,虽然在这之前甚至成为一个问题,你可能根本看不到任何实际的移动,因为 local 对象将被直接构造

You're guaranteed that local will be returned as an rvalue in this situation. Usually compilers would perform return-value optimization though before this even becomes an issue, and you probably wouldn't see any actual move at all, since the local object would be constructed directly at the call site.

在6.6.3 [返回语句](2)中的相关注意

A relevant Note in 6.6.3 ["The return statement"] (2):

为了说明,这就是说返回的对象可以从本地对象即使在实践中RVO将完全跳过此步骤)。标准的规范部分是12.8 [复制和移动类对象](31,32),对复制检测和右值(感谢@Mankarse!)。

To clarify, this is to say that the returned object can be move-constructed from the local object (even though in practice RVO will skip this step entirely). The normative part of the standard is 12.8 ["Copying and moving class objects"] (31, 32), on copy elision and rvalues (thanks @Mankarse!).

这是一个愚蠢的例子:

#include <utility>

struct Foo
{
    Foo()            = default;
    Foo(Foo const &) = delete;
    Foo(Foo &&)      = default;
};

Foo f(Foo & x)
{
    Foo y;

    // return x;         // error: use of deleted function ‘Foo::Foo(const Foo&)’
    return std::move(x); // OK
    return std::move(y); // OK
    return y;            // OK (!!)
}

与返回实际的右值引用进行对比:

Contrast this with returning an actual rvalue reference:

Foo && g()
{
    Foo y;
    // return y;         // error: cannot bind ‘Foo’ lvalue to ‘Foo&&’
    return std::move(y); // OK type-wise (but undefined behaviour, thanks @GMNG)
}

这篇关于返回的本地变量自动xvalues的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:41