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
14
votes
1 answer

emplacing a POD

Possible Duplicate: C++11 emplace_back on vector? Is emplacement possible with PODs? It does not seem to work in Visual Studio 2012: struct X { int a; int b; }; void whatever() { std::vector xs; X x = {1, 2}; //…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
13
votes
3 answers

performance of emplace is worse than check followed by emplace

I have a std::unordered_map with a value_type that does not have a default constructor so I cannot do the following auto k = get_key(); auto& v = my_map[k]; I ended up writing a helper function value_type& get_value(key_type& key) { return…
Jon
  • 1,785
  • 2
  • 19
  • 33
12
votes
3 answers

Emplace an aggregate in std::vector

I tried to initialize the std::vector std::vector particles; with instances of the simple struct struct Particle { int id; double x; double y; double theta; double weight; }; by using emplace with an initializer…
Oblomov
  • 8,953
  • 22
  • 60
  • 106
12
votes
4 answers

Is there any C# analogue of C++11 emplace/emplace_back functions?

Starting in C++11, one can write something like #include #include struct S { S(int x, const std::string& s) : x(x) , s(s) { } int x; std::string s; }; // ... std::vector v; // add new…
Constructor
  • 7,273
  • 2
  • 24
  • 66
10
votes
3 answers

Emplacing in vector using default constructor

I want to use vector::emplace to default construct a non-copyable and non-assignable object and then use specific methods on the object using an iterator to the newly created object. Note that there are no parameterized constructors of the class…
Chizzy C
  • 181
  • 1
  • 1
  • 8
9
votes
3 answers

simple structs with make_unique and emplace_back

Given a struct: struct S { int x; int y; } Why the Standard allows us to do this: std::vector vec; vec.emplace_back(1, 2); But does not allow to do this: auto ptr = std::make_unique(1, 2); ?
vladon
  • 8,158
  • 2
  • 47
  • 91
8
votes
2 answers

std::vector::emplace_back with lvalue expression

Does it ever make practical sense to use emplace_back with lvalue of some struct S: like this: std::vector v; auto s = S(/*...*/); v.emplace_back(s); Instead of just: v.emplace_back(/* S constructor arguments */); or is it just plain misuse of…
Student4K
  • 916
  • 2
  • 9
  • 20
8
votes
1 answer

How can I try_emplace a POD struct in a std::map?

I have a map of int -> { basic types } I need to store. I would like to simple create a struct { int f1, int f2; }; and store the values directly, constructing the struct in situ during the store. I do not expect there to be any repeated keys, so…
aghast
  • 14,785
  • 3
  • 24
  • 56
8
votes
1 answer

Why is move necessary with emplace_back in this example?

The following minimal working example compiles when the code under option 1 or option 2 is used, but does not compile when the code under option 3 is used. I was under the assumption that emplace_back() implicitly uses/calls a move constructor, so…
AnInquiringMind
  • 773
  • 1
  • 7
  • 13
8
votes
2 answers

How to implement a simple container with placement new and emplace functionality?

I need to implement a container to hold an amount of elements and for some reason, it has to work without any heap allocation. Another requirement is, that the container elements should not be copied or moved in any way. They have to constructed…
Joe
  • 3,090
  • 6
  • 37
  • 55
7
votes
1 answer

Is it possible to emplace a std::array in a container? If it is how? If not, why?

Given the simple code #include #include int main() { std::vector> v; v.emplace_back(std::array{1,2,3}); } I'm first of all worried about what is really going on. My understanding of emplace_back is…
Enlico
  • 23,259
  • 6
  • 48
  • 102
7
votes
2 answers

emplace_back() vs push_back when inserting a pair into std::vector

I defined the following std::vector > my_vec; my_vec.push_back( {1,2} ); //this works my_vec.emplace_back( {1,2} ); // this doesn't work std::pair temp_pair = {1,2}; my_vec.emplace_back( temp_pair ); //this…
24n8
  • 1,898
  • 1
  • 12
  • 25
6
votes
1 answer

std::map try emplace vs emplace strange behaviour

The common advice is to prefer using std::map::try_emplace in preference to std::map::emplace in almost every instance. I wrote a simple test to trace object creation/copying/moving/destruction when calling those functions, with and without clashes,…
Alex O
  • 1,429
  • 2
  • 13
  • 20
6
votes
1 answer

Why does Clang 12 refuse to initialize aggregates in the C++20 way?

As far as I understand, the following program should work in C++20 mode: #include struct B{ int a0, a1; }; int main() { std::vector bs; bs.emplace_back( 0, 0 ); } And it really does in Visual Studio 2019 and gcc 11. But not…
Fedor
  • 17,146
  • 13
  • 40
  • 131
6
votes
2 answers

How to emplace elements while constructing std::vector?

I want to construct an std::vector with some elements having these elements constructed by some particular constructor rather than the default constructor. In other words I want to emplace the elements while constructing the vector. How can I do…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
1
2
3
12 13