Construct-in-place an unmoveable object in a map

送分小仙女□ 提交于 2019-12-01 17:52:47

map.emplace(std::piecewise_construct, std::make_tuple(0), std::make_tuple()) will construct a zero-argument Z at location 0.

map[0] will also do it if it is not already there.

emplace takes the arguments to construct a std::pair<const K, V>. std::pair has a std::piecewise_construct_t tagged constructor that takes two tuples, the first is used to construct the first argument, the second to construct the second argument.

so std::pair<const int, Z> test( std::piecewise_construct, std::make_tuple(0), std::make_tuple() ) constructs the tests elements in-place, the const int is constructed with (0). The Z is constructed with ().

map.emplace forwards is arguments to the std::pair constructor.

The simplest solution is to use operator[] to construct the value inside the map. Then you can assign a value (or operate on it as needed).

May be the following solution will be better, since atomic is not copyable:

class Z {
  std::atomic<int> i;
};

std::unordered_map<int, std::shared_ptr<Z>> map;

void test(void) {
  map.emplace(0, std::make_shared<Z>()); // OK
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!