Skip to content

Instantly share code, notes, and snippets.

@XueshiQiao
Last active February 12, 2021 17:48
Show Gist options
  • Save XueshiQiao/c30f30d57ee582e5635c6fb42bd0c2bd to your computer and use it in GitHub Desktop.
Save XueshiQiao/c30f30d57ee582e5635c6fb42bd0c2bd to your computer and use it in GitHub Desktop.
C++ std:move usage sample
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
class SimpleString {
public:
SimpleString(int s, const char* content): size(s+1), string(new char[s+1]) {
for (int i = 0; i < size - 1; i++) {
string[i] = content[i];
}
string[size-1] = '\0';
}
SimpleString(const SimpleString& a) {
cout << "COPY cons get called" << endl;
size = a.size;
string = new char[size+1];
for (int i = 0; i < size; i++) {
string[i] = a.string[i];
}
}
SimpleString(SimpleString && a): size(a.size) {
cout << "MOVE cons get called" << endl;
string = a.string;
a.string = nullptr;
a.size = 0;
}
static void test(const SimpleString & a) {
cout << "test(const SimpleString& a) get called" << endl;
}
static void test(SimpleString && a) {
cout << "test(SimpleString && a) get called" << endl;
}
void print(const char * label) {
cout << label << ":" << (size > 0 ? string : "<empty>") << ", size:" << size << endl;
}
private:
char *string = nullptr;
int size = 0;
};
SimpleString tempString() {
return SimpleString(5, "hello");
}
void printSeparateLine(string label) {
cout << endl << "========= " << label << " ========" << endl;
}
int main(int argc, char *argv[]) {
SimpleString a0(7, "abcdefg");
printSeparateLine("SimpleString a1 = a0");
SimpleString a1 = a0;
a0.print("a0");
a1.print("a1");
printSeparateLine("SimpleString a2 = std::move(a1)");
SimpleString a2 = std::move(a0);
a0.print("a0");
a2.print("a2");
printSeparateLine("SimpleString a3 = static_cast<SimpleString&&> (a2)");
SimpleString a3 = static_cast<SimpleString&&> (a2);
a2.print("a2");
a3.print("a3");
printSeparateLine("SimpleString a4 = a1");
SimpleString a4 = a1;
SimpleString::test(a4);
SimpleString::test(std::move(a4));
//std::move(a) 等价于 static_cast<SimpleString &&>(a), C++ doc:
//In particular, std::move produces an xvalue expression that identifies its argument t.
//It is exactly equivalent to a static_cast to an rvalue reference type.
SimpleString::test(static_cast<SimpleString&&>(a4));
a4.print("a4");
printSeparateLine("SimpleString a5 = tempString()");
SimpleString::test(tempString()); //tempString() 返回的是一个右值
printSeparateLine("\b=======================");
printSeparateLine("test via vector.push_back()");
vector<SimpleString> strings;
strings.reserve(10);
SimpleString a5(11, "hello world");
cout << endl << "call strings.push_back(a5)" << endl;
strings.push_back(a5);
a5.print("a5");
cout << endl << "call strings.push_back(std::move(a5))" << endl;
strings.push_back(std::move(a5));
a5.print("a5_after_move");
}
@XueshiQiao
Copy link
Author

XueshiQiao commented Feb 12, 2021

Output with clang compiler:

========= SimpleString a1 = a0 ========
COPY cons get called
a0:abcdefg, size:8
a1:abcdefg, size:8

========= SimpleString a2 = std::move(a1) ========
MOVE cons get called
a0:<empty>, size:0
a2:abcdefg, size:8

========= SimpleString a3 = static_cast<SimpleString&&> (a2) ========
MOVE cons get called
a2:<empty>, size:0
a3:abcdefg, size:8

========= SimpleString a4 = a1 ========
COPY cons get called
test(const SimpleString& a) get called
test(SimpleString && a) get called
test(SimpleString && a) get called
a4:abcdefg, size:8

========= SimpleString a5 = tempString() ========
test(SimpleString && a) get called

================================ ========

========= test via vector.push_back() ========

call strings.push_back(a5)
COPY cons get called
a5:hello world, size:12

call strings.push_back(std::move(a5))
MOVE cons get called
a5_after_move:<empty>, size:0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment