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.

136 questions
222
votes
5 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
  • 4,283
  • 5
  • 17
  • 33
97
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
  • 13,698
  • 3
  • 55
  • 59
58
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
  • 266,361
  • 143
  • 616
  • 705
43
votes
1 answer

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
  • 66,800
  • 33
  • 152
  • 256
42
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
  • 463
  • 1
  • 5
  • 6
23
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
  • 22,881
  • 2
  • 66
  • 85
22
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,441
  • 1
  • 15
  • 24
21
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
  • 97,781
  • 45
  • 291
  • 525
20
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
  • 9,858
  • 10
  • 52
  • 88
20
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,379
  • 4
  • 12
  • 18
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
  • 825
  • 6
  • 21
17
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
  • 201
  • 2
  • 7
16
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
  • 69,525
  • 30
  • 173
  • 306
13
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
  • 237,063
  • 85
  • 359
  • 638
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
  • 6,903
  • 2
  • 17
  • 55
1
2 3
9 10