我怎么知道我使用的是复制还是移动?

bli*_*ppy 6 c++ move-semantics c++14

我是否理解 C++14 标准库使用移动语义?换句话说,我是否可以确信我在以下程序中使用的是移动而不是副本:

#include <iostream>
#include <string>
#include <vector>

using namespace std::string_literals;

std::vector<std::string> greeting()
{
    std::vector<std::string> vs {"hello"s, "world"s};
    return vs;
}

int main()
{
    std::vector<std::string> s = greeting();
    std::cout << s[0] << " " << s[1] << "\n" ;
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以检查吗?

在下面的例子中怎么样:

#include <iostream>
#include <string>
#include <vector>

using namespace std::string_literals;

class Greeting {
    public:
    std::string first, second;
    Greeting() { first = "hello"s ; second = "world"s ;};
};

Greeting greetingc()
{
    Greeting g;
    return g;
}

int main()
{
    Greeting g = greetingc();
    std::cout << g.first << " " << g.second << "\n" ;
}
Run Code Online (Sandbox Code Playgroud)

移动还是复制?

Mat*_*tzi 2

在大多数情况下,复制和移动之间没有太大区别。只有当您拥有不想复制的东西的所有权时,这才有意义。就像分配给对象的套接字或内存一样。因此,只有当某些东西既昂贵(例如当您只需要其中一个时复制一大块内存)并且您必须处理所有权(没有两个指针指向同一内存或套接字等时才有趣)。 .)。

在您的两个示例中,最有可能发生的情况是编译器将执行 RVO 返回值优化,这消除了复制或移动的需要。Vector 定义了 move,因此编译器将尽可能使用 move 语义(右值语义),并且您可以使用 std::move 强制它。但你的例子中的邻居会因此而更快。阅读有关移动的更多信息

如果您好奇,您可以实现复制和移动,并将写入内容从它们写入控制台。

Greeting(const Greeting& g)
{
    std::cout << "Copy";
    // Copy it
}

Greeting(Greeting&& g)
{
    std::cout << "Move";
    // Move it
}
Run Code Online (Sandbox Code Playgroud)

通常会发生这种情况。

Greeting foo(){ Greeing a; return a; }
Greeting a; // Normal construction
Greeting b(a); // Copy of 'a'
Greeting c(std::move(a)); // Move, 'a' will be changed
Greeting d(foo()); // Move from rvalue which is returned from foo() unless RVO kicks in
Run Code Online (Sandbox Code Playgroud)