Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emplace object with no-argument constructor into std::map?

Tags:

c++

c++11

I want to emplace an object into a std::map whose constructor does not take any arguments. However, std::map::emplace seems to require at least one additional argument besides the key. So how can I forward zero arguments to the constructor?

like image 236
Inkane Avatar asked Dec 18 '14 18:12

Inkane


2 Answers

The element type of std::map<K, V> is actually std::pair<K, V>, so when you are emplacing into a map, the arguments will be forwarded to the constructor of std::pair. That's why you can't pass just the key: std::pair<K, V> can't be constructed from a single argument (unless it's another pair of the same type.) You can pass zero arguments, but then the key will be value-initialized, which is probably not what you want.

In most cases, moving values will be cheap (and keys will be small and copyable) and you should really just do something like this:

M.emplace(k, V{}); 

where V is the mapped type. It will be value-initialized and moved into the container. (The move might even be elided; I'm not sure.)

If you can't move, and you really need the V to be constructed in-place, you have to use the piecewise construction constructor...

M.emplace(std::piecewise_construct, std::make_tuple(k), std::make_tuple()); 

This causes std::pair to construct the first element using k and the second element using zero arguments (value-initialization).

like image 71
Brian Bi Avatar answered Sep 20 '22 15:09

Brian Bi


You could explicitly create a pair and pass that to map::emplace, or use the piecewise construction constructor of std::pair.

struct foo {};  std::map<int, foo> m;  m.emplace(std::pair<int, foo>(1, {})); m.emplace(std::piecewise_construct,           std::forward_as_tuple(2),           std::forward_as_tuple()); 

Live demo

like image 32
Praetorian Avatar answered Sep 19 '22 15:09

Praetorian