Last active
September 4, 2020 09:44
-
-
Save syohex/cfe3cc8fb68b77c4e427fc2230ab5ad6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <string> | |
#include <vector> | |
struct Foo { | |
Foo() = default; | |
Foo(const Foo &f) : s(f.s) { | |
std::cout << "Copy constructor" << std::endl; | |
} | |
Foo(Foo &&f) : s(std::move(f.s)) { | |
std::cout << "Move constructor" << std::endl; | |
} | |
std::string s; | |
}; | |
Foo getFoo() { | |
Foo f; | |
return f; | |
} | |
const Foo getFooConst() { | |
Foo f; | |
return f; | |
} | |
int main() { | |
std::vector<Foo> f; | |
f.reserve(32); // avoiding reallocation at second push_back | |
f.push_back(getFoo()); // call move constructor | |
f.push_back(getFooConst()); // call copy constructor | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment