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
2
votes
3 answers

std::map::emplace fails to resolve, but insert of rvalue works -- why?

Consider the following attempt to emplace an empty vector with a numeric key in a map: #include #include int main () { std::map> m; m.emplace(42, {}); // FAILS …
Jonas Greitemann
  • 1,011
  • 10
  • 25
2
votes
0 answers

Implement emplace in open adressing hash table in C++

I'm implementing an open address hash table like this: template class open_addressing_map { public: using key_type = K; using mapped_type = V; using value_type = std::pair; …
Phạm Văn Thông
  • 743
  • 2
  • 7
  • 21
2
votes
2 answers

Why does vector::emplace_back, such that T has a deleted copy constructor, fail to compile?

I cannot compile the following dont_compile function. I don't understand why it doesn't work. But, it does work with list. class Thing { public: Thing() {} Thing(const Thing &) = delete; }; int dont_compile(int argc, char ** argv) { …
ioquatix
  • 1,411
  • 17
  • 32
2
votes
2 answers

Odd std::vector::emplace() compilation error

I've come across a strange compiler error while using std::vector::emplace() and std::vector::emplace_back(): #include struct Foo { int bar; Foo(int _bar) : bar(_bar) { } }; int main() { // Declaration 1 std::vector
JamesNZ
  • 187
  • 3
  • 7
2
votes
1 answer

Is it possible to emplace an instance inside std::optional w/o checking/destructing the previous one?

Let's say we have a variable std::optional x; of type std::optional for some type T. If I want to call a constructor of T inside x and initialize it, I can call the member function std::optional::emplace. As I know, this member function…
Junekey Jeon
  • 1,496
  • 1
  • 11
  • 18
2
votes
1 answer

Is c++11 operator[] equivalent to emplace on map insertion?

For C++11, is there still a performance difference between the following? (for std::map > as an example) map[key] = myVector and map.emplace(key, myVector) The part I'm not figuring out is the exact internal of operator[]. My…
Superziyi
  • 609
  • 1
  • 9
  • 30
2
votes
3 answers

Efficiently instert tuple into container through move

I'm a move semantics beginner. Is this code: template void foo(const Args & ... args){ map, int> cache; auto result = cache.emplace(move(make_tuple(args ...)),1); //... } More efficient…
justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
2
votes
1 answer

Unable to emplace_back instance of a class with const members

I have a problem using 'emplace_back' on an instance of a class with const members. See a sample code listing below. #include using std::vector; class Data { public: Data(int a, int b, int c) : a(a), b(b), c(c) {} // Removing…
WaelJ
  • 2,942
  • 4
  • 22
  • 28
1
vote
1 answer

Aggregate initialization inside emplace_back

In the following code, I am trying to use aggregate initialization with emplace_back. The catch here is that emplace_back takes the constructor arguments and constructs the object directly in the vector, and I want to know whether the copy…
1
vote
1 answer

What is happening in std::vector::emplace() implementation?

I implement my own vector class just to practice my C++ skill and my emplace() method is way slower than std::vector::emplace() so I checked the source code to see how they implemented it but it is just pain for me since that. I can't wrap my head…
1
vote
1 answer

Emplacing an instance with constant fields fails to compile (C++)

I have an example_class with two constant fields. When I try to emplace an object of that class into a std::vector, I get various errors such as "error: object of type 'example_class' cannot be assigned because its copy assignment operator is…
1
vote
0 answers

Why std::set does not provide try_emplace member function?

One advantage of try_emplace member function of std::map (and std::unordered_map) is that it does not allocate a new node if the key already exists in the map. I wonder why this member function has not been added to the std::set (and…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
1
vote
1 answer

Emplacing an std::pair of strings to an unordered map reusing the string's heap

I have an unordered map of string-pairs that i reference via a message id (key). Now I want to construct the string pair in place using the temporary string objects that the function receives, hence reusing the memory already allocated for the…
glades
  • 3,778
  • 1
  • 12
  • 34
1
vote
1 answer

Emplace a std::array of non-movable objects that cannot be default constructed

I have a class that contains a std::mutex so it is not movable or copiable. struct MyObject { MyObject(std::string s_) : s(s_) {}; std::mutex lock; std::thread worker; std::string s; }; I can easily add this object to this map: …
TheBat
  • 1,006
  • 7
  • 25
1
vote
1 answer

Erasing a tuple from a set with tuple and custom sort function

How can we remove elements from a set like below which has iterator as part of its key? .erase() throws error saying no matching member function for call to 'erase'. Just like emplace() can be used for inserting tuples, what can be used for erasing…
Bharat MV
  • 23
  • 8