C++ Move

How to use C++ move?

Just like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <vector>

void fun(std::string& str)
{
std::cout << "l-value reference, str=" << str << std::endl;
std::string temp_str(std::move(str));
std::cout << "temp_str = move(str), now str=" << str << std::endl;
std::cout << "temp_str=" << temp_str << std::endl;
}

void fun(std::string&& str)
{
std::cout << "get r-value reference, build a lvalue str" << std::endl;
std::cout << "now str=" << str << std::endl;
std::string temp_str(std::move(str));
std::cout << "temp_str = move(str), now str=" << str << std::endl;
std::cout << "temp_str=" << temp_str << std::endl;
}

void array_move()
{
std::cout << "array move test" << std::endl;
std::vector<char> array = {'h', '2'};
std::vector<std::vector<char>> array_vector;
array_vector.push_back(std::move(array));
std::cout << "after move, the size of array :" << array.size() << std::endl;
}

int main()
{
std::string str = "hello,world";
fun(str);
std::cout << "after move main: str=" << str << std::endl;
fun("good");
array_move();
return 0;
}