I am trying to understand why do I get an extra move constructor operation when adding my resource management objects to vector.
Here is my resource management class:
class Resource
{
public:
Resource() {
own_ = true;
id_ = next_id++;
std::cout << "Resource " << id_ << " constructed\n";
}
Resource(Resource&& c) {
std::cout << "Move constructed from " << c.id_ << " to " << id_ << "\n";
swap(*this, c);
}
~Resource() {
if (own_) {
std::cout << "Resource " << id_ << " destructed\n";
}
}
Resource& operator=(Resource&& c) {
std::cout << "Move assigned from " << c.id_ << " to " << id_ << "\n";
swap(*this, c);
return *this;
}
void swap(Resource& a, Resource& b) {
std::swap(a.id_, b.id_);
std::swap(a.own_, b.own_);
}
Resource(Resource const&) = delete;
auto operator=(Resource const&)->Resource = delete;
static int next_id;
private:
bool own_ = false;
int id_;
};
int Resource::next_id = 1;
And here is the main function where I am creating two objects and moving them to my vector of resources:
int main() {
std::vector<Resource> resources;
Resource a, b;
resources.push_back(std::move(a));
resources.push_back(std::move(b));
return 0;
}
And here is the output. First 4 lines are like I expected them to be, but last line seems to be extra operation like something is being rearranged or sorted inside a vector. Final contents of the vector are as expected though - 2 resource objects but my output is:
Resource 1 constructed
Resource 2 constructed
Move constructed from 1 to -842150451
Move constructed from 2 to -842150451
Move constructed from 1 to -842150451