What is std::move() used for?

Asked 1 years ago, Updated 1 years ago, 84 views

The question I want to ask is std::move()

This is.

c++ c++11 move-semantics

2022-09-22 22:29

1 Answers

std::move() in C++ is written to avoid copy. For example, the following two swap functions behave differently:

template <class T>
swap1(T& a, T& b) {
    Ttmp(a); // have two copies of a.
    a = b; // become two copies of b (one of the copies of + a disappears)
    b = tmp; // 2 copies of tmp (one of the copies of +b disappears)
}

If you use //move, you can swap without copying
template <class T>
swap2(T& a, T& b) {
    T tmp(std::move(a));
    a = std::move(b);   
    b = std::move(tmp);
}


2022-09-22 22:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.