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
289
votes
6 answers

insert vs emplace vs operator[] in c++ map

I'm using maps for the first time and I realized that there are many ways to insert an element. You can use emplace(), operator[] or insert(), plus variants like using value_type or make_pair. While there is a lot of information about all of them…
German Capuano
  • 5,183
  • 6
  • 23
  • 35
108
votes
2 answers

Efficiency of C++11 push_back() with std::move versus emplace_back() for already constructed objects

In C++11 emplace_back() is generally preferred (in terms of efficiency) to push_back() as it allows in-place construction, but is this still the case when using push_back(std::move()) with an already-constructed object? For instance, is…
Riot
  • 15,723
  • 4
  • 60
  • 67
67
votes
2 answers

std::map emplace without copying value

The C++11 std::map type has an emplace function, as do many other containers. std::map m; std::string val {"hello"}; m.emplace(1, val); This code works as advertised, emplacing the std::pair directly, however it results…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
57
votes
2 answers

What is the difference between unordered_map::emplace and unordered_map::insert in C++?

What is the difference between std::unordered_map::emplace and std::unordered_map::insert in C++?
Harsh M. Shah
  • 613
  • 1
  • 6
  • 6
48
votes
2 answers

Why emplace_back is faster than push_back?

I thought that emplace_back would be the winner, when doing something like this: v.push_back(myClass(arg1, arg2)); because emplace_back would construct the object immediately in the vector, while push_back, would first construct an anonymous object…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
31
votes
1 answer

why do i need to use piecewise_construct in map::emplace for single arg constructors of noncopyable objects?

The following code will not compile on gcc 4.8.2. The problem is that this code will attempt to copy construct an std::pair which can't happen due to struct A missing copy and move constructors. Is gcc failing here or am I missing…
oli_obk
  • 28,729
  • 6
  • 82
  • 98
29
votes
1 answer

Difference between 's emplace and push

What are the differences between 's emplace and push? here's explanation about the std::queue::emplace and std::queue::push . Both methods add element after its current last element, return None.
Dami.h
  • 321
  • 1
  • 3
  • 7
28
votes
3 answers

Emplacement of a vector with initializer list

i have a std::vector> and would like to add some elements at the end of it so this was my trial: std::vector > vec; vec.emplace_back({0,0}); but this does not compile whereas the following will…
m47h
  • 1,611
  • 2
  • 18
  • 26
26
votes
4 answers

Insert into vector having objects without copy constructor

I have a class whose copy constructors are explicitly deleted (because A uses pointers internally and I don't want to fall into shallow copy pitfalls): class A { public: A(const A&) = delete; A& operator=(const A&) = delete; A(const…
vigs1990
  • 1,639
  • 4
  • 17
  • 19
24
votes
5 answers

How do I use std::vector::emplace_back with vector>?

vector> res; res.emplace_back({1,2}); // change to res.push_back({1,2}); would work This gives me the error: main.cpp:61:25: error: no matching function for call to ‘std::vector >::emplace_back(
user40129
  • 763
  • 1
  • 5
  • 15
22
votes
3 answers

Why doesn't this use of emplace_back with deleted copy constructor work?

I have a type that I have deleted the copy constructor from, and I would like to have a vector of this type, so I need to create all the elements via emplace_back. But, emplace_back seems to require a copy constructor, as the compiler gives a…
Drew
  • 12,578
  • 11
  • 58
  • 98
22
votes
1 answer

Is it safe to use emplace_back with a container of unique_ptrs?

Consider the following: std::vector> ptrsToInts; ptrsToInts.emplace_back(new int); If reallocation occurs in the vector, and that fails (throwing std::bad_alloc), am I "safe" or will I leak an int? C++11 23.3.6.5…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
21
votes
2 answers

Why can an aggreggate struct be brace-initialized, but not emplaced using the same list of arguments as in the brace initialization?

It seems like this code: #include #include struct bla { std::string a; int b; }; int main() { std::vector v; v.emplace_back("string", 42); } could be made to work properly in this case, but it doesn't (and I…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
19
votes
1 answer

Best way to use emplace_back to avoid move constructor call?

I just learned about guaranteed copy elision in C++17. According to the answer on that question: When you do return T();, this initializes the return value of the function via a prvalue. Since that function returns T, no temporary is created;…
user2108462
  • 855
  • 7
  • 23
15
votes
1 answer

map::emplace where V constructor requires two parameters

All the examples I can find for map::emplace use fairly simple types that have constructors that take a single argument, e.g. std::map foo; foo.emplace("a", "b"); Suppose the value type constructor requires two parameters.…
jzions
  • 431
  • 3
  • 9
1
2 3
12 13