C++ Emplacing

Emplace operations are provided to make C++ code faster. They should be used when you want to add an object by constructing at the point of calling the emplace operation. For example, you may want to construct the object using its default constructor and then push it into the container. Or you may want to construct it using some parameters on the spot. Formerly, this involved the construction of a temporary object and then copying it into the container.

Many of the STL containers support emplacing. For example, vector now has emplace_back operation along with the old push_back operation, as well as emplace along with insert.

The good news is that the syntax of emplace and push/insert operations are the same, yet the efficiency of the former is much higher.

Emplace members forward the constructor's arguments in an automagical fashion. Here is an example:

#include <iostream>
#include <utility>
#include <string>
#include <unordered_map>

int main()
{
  std::unordered_map<unsigned int, std::string> m;
  m.emplace(std::make_pair(33, std::string("blood-curdling")));
  m.emplace(17, "built-in");

  for (const auto &p : m) {
    std::cout << p.first << " => " << p.second << '\n';
  }

  return 0;

Note: emplace operations seem to bypass ordinary constructors and use the copy constructor instead.