Created
June 18, 2017 05:30
-
-
Save kmcallister/5b13717f1552225019b21d79192ca7d3 to your computer and use it in GitHub Desktop.
std::vector resizing with copies?
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 <vector> | |
struct Foo { | |
Foo() { } | |
Foo(const Foo &x) { | |
std::cout << "Copied!" << std::endl; | |
} | |
Foo(Foo &&x) { | |
std::cout << "Moved!" << std::endl; | |
} | |
}; | |
int main() { | |
auto vec = std::vector<Foo>(); | |
for (int i=0; i<10; i++) { | |
std::cout << "Capacity: " << vec.capacity() << std::endl; | |
vec.push_back(Foo()); | |
} | |
return 0; | |
}>> |
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
$ g++ -Wall -Werror -o foo foo.cc --std=c++14 && ./foo | |
Capacity: 0 | |
Moved! | |
Capacity: 1 | |
Moved! | |
Copied! | |
Capacity: 2 | |
Moved! | |
Copied! | |
Copied! | |
Capacity: 4 | |
Moved! | |
Capacity: 4 | |
Moved! | |
Copied! | |
Copied! | |
Copied! | |
Copied! | |
Capacity: 8 | |
Moved! | |
Capacity: 8 | |
Moved! | |
Capacity: 8 | |
Moved! | |
Capacity: 8 | |
Moved! | |
Copied! | |
Copied! | |
Copied! | |
Copied! | |
Copied! | |
Copied! | |
Copied! | |
Copied! | |
Capacity: 16 | |
Moved! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment