Menu
  • HOME
  • TAGS

How to use emplace() in a std::map whose value is a std::set (map from something to a set)?

c++,c++11,stdmap,stdset,emplace

Braced initializer lists have no type, so they can't be perfectly forwarded. In this particular case, you can specify the type (std::initializer_list<int>) explicitly if you want everything to be constructed in place: misi.emplace( std::piecewise_construct, std::forward_as_tuple(2345), std::forward_as_tuple(std::initializer_list<int>{6, 9}) ); Since only one argument each is passed for the key and the...

emplace unordered_set in unordered_map

c++,c++11,move,unordered-map,emplace

Braced initializers are one of the edge cases that perfect forwarding is not so perfect about. The issue is that braced initializers passed to function template parameters are in a non-deduced context and compilers are not allowed to deduce a type for them. Luckily, the fix is pretty easy: just...

Emplacing in vector using default constructor

c++,c++11,vector,emplace

As pointed out by @dyp and @Casey in the comments, std::emplace will not work for vectors of the Test class as the class is also not movable because "the user-declared copy constructor suppresses generation of the default move constructor" (@Casey). To use emplace here, the class will need to be...

priority_queue::emplace calls push_heap?

c++,performance,priority-queue,emplace

The implementation certainly doesn't have to call the algorithm but probably it does. After emplacing() the element it will be move towards the root until the heap invariant is restored. Note that push_heap() does not touch the entire heap: that would be make_heap(). Instead, push_heap() deals only with adding one...

Custom Container emplace with variadic templates

c++,variadic-templates,emplace

In case anyone stumbles upon the same issue, this is how I achieved this: void emplace(Args&&... args) { ++Count; ++_indexWrite; if (_indexWrite > _size - 1) _indexWrite = 0; _v.emplace(_v.begin() + _indexWrite, std::forward<Args>(args)...); } Although what I really wanted was to construct an element using the reserved memory in that...

how to emplace_back(pair) efficiently?

c++,std-pair,emplace

std::array<T, N> doesn't have any constructor taking a std::initializer_list<U> for any U, not even for U = T. Don't mistake the fact that it can be initialised from a braced-init-list for the presence of such a constructor. Instead, actually create an array. foo.emplace_back(std::array<int,3>{i,j,k}, q); There are hardly any guarantees about...

std::map emplace without copying value

c++,c++11,dictionary,stl,emplace

The arguments you pass to map::emplace get forwarded to the constructor of map::value_type, which is pair<const Key, Value>. So you can use the piecewise construction constructor of std::pair to avoid intermediate copies and moves. std::map<int, Foo> m; m.emplace(std::piecewise_construct, std::forward_as_tuple(1), std::forward_as_tuple(2.3, "hello")); Live demo...

Address of an instance emplaced to std::vector is invalid

c++11,vector,standards,emplace

Two options: 1) You can simply fix your test. You just need in you test preallocate enough memory first with vec1.reserve(10); Well, this is implementation details for std::vector. As more and more items are added to std::vector it needs to get more space for them. And this space must be...