Last active
February 12, 2021 17:48
-
-
Save XueshiQiao/c30f30d57ee582e5635c6fb42bd0c2bd to your computer and use it in GitHub Desktop.
C++ std:move usage sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output with
clang
compiler: