Search code examples
c++movemove-semantics

Why is movement constructor invoked twice while std::emplace_back is called only once?


I understand that std::emplace_back uses placement-new to construct the element in-place at the location provided by the container.

Why is movement constructor invoked twice while std::emplace_back is called only once?

Here is the related code(check https://godbolt.org/z/-NXzNY):

#include <vector>
#include <string>
#include <iostream>

struct President
{
    std::string name;
    std::string country;
    int year;

    President(std::string p_name, std::string p_country, int p_year)
        : name(std::move(p_name)), country(std::move(p_country)), year(p_year)
    {
        std::cout << "I am being constructed.\n";
    }
    President(President&& other)
        : name(std::move(other.name)), country(std::move(other.country)), year(other.year)
    {
        std::cout << "I am being moved.\n";
    }
    President& operator=(const President& other) = default;
};

int main()
{
    std::vector<President> elections;
    std::cout << "emplace_back:\n";
    elections.emplace_back("Nelson Mandela", "South Africa", 1994);

    President pst("Franklin", "the USA", 1936);

    std::cout << "=====================" << std::endl;
    elections.emplace_back(std::move(pst));
}

Solution

  • You have two objects; Putting the second to the vector causes a vector resize, memory reallocation, hence the movement requirement. In addition to construction-in-place that emplace_back does, that's another point of the move construction: To avoid expensive copies when std::vector resizes.

    When adding this line to your code:

    elections.reserve(2);
    

    Then you have only one movement.