Questions tagged [emplace]

For questions that concern constructing objects directly within some container object, as opposed to copying/moving the object into the container.

Use for questions related to the act of doing something in place, like for example inserting a new element in place.

For example, for an std::vector in , one could use std:emplace_back(), which is described like:

Inserts a new element at the end of the vector, right after its current last element. This new element is constructed in place using args as the arguments for its constructor.

183 questions
1
vote
1 answer

emplace_back() vs push_back() for vector

I knew this has been asked quite a lot, and I have seen many explanations quoting "emplace_back construct in place, push_back() construct and copy". Some posts asked why emplace_back calls the copy constructor because they did not reserve the memory…
yuhao
  • 71
  • 1
  • 5
1
vote
2 answers

How to vector::emplace_back a class that has a shared_mutex?

How can I emplace_back a class that has a shared_mutex? It looks like that a shared_mutex makes a class no longer MoveConstructible/CopyConstructible. Compiling my code gives me result type must be constructible from value type of input range. And…
1
vote
1 answer

emplace_back to std::vector not works

I wondered that such simple program can not be compiled with gcc, clang and msvc: #include #include int main() { std::vector> v; v.emplace_back( 1, 2, 3 ); return 0; } Why do std::array can not be…
Ivan Ivlev
  • 332
  • 1
  • 9
1
vote
1 answer

Should stdlib classes with emplace-mechanisms be friendable?

I am wondering if this an stdlib bug, an oversight, my own error or intended by the standards committee. If a type T's constructor (any of its constructors) is non-public, an unrelated container cannot emplace an element using that constructor,…
bitmask
  • 32,434
  • 14
  • 99
  • 159
1
vote
2 answers

Insert vs. emplace_back for appending new element to vector/list if you want an iterator to the newly inserted element

I understand the general differences between insert and emplace_back for std::vector and std::list. Normally, I would just use emplace_back any time I want to append an element. For some applications, I need to append the element and have an…
24n8
  • 1,898
  • 1
  • 12
  • 25
1
vote
1 answer

How to detect implicit conversion losses integer precision on std::vector::emplace_back

I compiled the following code with -Wconversion compiler option to detect implicit conversion loses integer precision: #include #include int main() { std::vector v; std::uint32_t a = 0; …
Takatoshi Kondo
  • 3,111
  • 17
  • 36
1
vote
1 answer

How to create an emplace_back method?

I´m creating a custom ArrayList/Vector class and I´m having troubles creating an emplace_back function. How can I create arguments that are the same as the constructor of the "value_type class" of the ArrayList ?
1
vote
1 answer

Emplace object derived from non-copyable into vector

I have objects that holds pointers and shares them with others. Moving them is fine, but copying not. I would like to store them in vectors and maps. The following code works only when A has no destructor. However I would need a destructor to clean…
ypnos
  • 50,202
  • 14
  • 95
  • 141
1
vote
0 answers

varadic argument templated object creation on container not working

Below is a example (not the entire class, just relevant parts) class Container { private: size_t m_next_id; std::unordered_map m_objects; public: template size_t create(Args... args) { return…
1
vote
1 answer

Why does unordered_map's emplace with piecewise_construct argument needs default constructor?

I have an unordered_map which stores pairs. I wanted to emplace pairs with this snippet: map.emplace(std::piecewise_construct, std::forward_as_tuple(name), std::forward_as_tuple(constructorArg1,…
Tudvari
  • 2,715
  • 2
  • 14
  • 33
1
vote
2 answers

multiple emplace_back calling copy constructor additionally

Consider the below example : #include #include class S { public: S() { puts("S()"); } S(int) { puts("S(int)"); } ~S() { puts("~S()"); } S(const S &) { puts("S(const S&)"); } S(S &&) { puts("S&&"); } const S…
Sitesh
  • 1,816
  • 1
  • 18
  • 25
1
vote
1 answer

Figure out whether an item has been added or not when using emplace_hint

Since emplace_hint method of set / map returns only an iterator figuring out whether item has been actually added is not as convenient as when using emplace or insert. Right now I just get container size before / after and compare…
user7860670
  • 35,849
  • 4
  • 58
  • 84
1
vote
2 answers

Emplace for complex objects

I am trying to implement an "emplace" feature in an object. It is structured like below. I have a template object that pairs a size_t to a template type. I would like to be able to construct this in place in a std library container, e.g., a vector.…
pippin1289
  • 4,861
  • 2
  • 22
  • 37
1
vote
1 answer

Emplace a derived movable but noncopyable in a vector gives compilation error

I'm having compiler errors when trying to emplace_back in a vector of non-copyable but movable objects with a subtle inheritance twist, that should to my knowledge not change the problem. Is this legal C++ and this a Visual Studio 2015 bug, or am I…
N0vember
  • 995
  • 1
  • 9
  • 12
1
vote
1 answer

Inserting an object having a non copyable field into an std::vector

I understand that the following code does not compile since the move constructor of A is deleted because the mutex is not movable. class A { public: A(int i) {} private: std::mutex m; }; int main() { std::vector v; …
user695652
  • 4,105
  • 7
  • 40
  • 58