Skip to content

Instantly share code, notes, and snippets.

@syohex
Last active September 4, 2020 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syohex/cfe3cc8fb68b77c4e427fc2230ab5ad6 to your computer and use it in GitHub Desktop.
Save syohex/cfe3cc8fb68b77c4e427fc2230ab5ad6 to your computer and use it in GitHub Desktop.
#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