Construct-in-place an unmoveable object in a map

后端 未结 3 971
清酒与你
清酒与你 2021-01-18 14:36

I\'m trying to construct an object in a map that contains an atomic, so it can neither be copied nor moved AFAICT.

My reading of C++ reference is that map empl

相关标签:
3条回答
  • 2021-01-18 15:09

    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.

    0 讨论(0)
  • 2021-01-18 15:16

    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).

    0 讨论(0)
  • 2021-01-18 15:17

    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
    }
    
    0 讨论(0)
提交回复
热议问题