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

How do I insert an element to a set that is a value in multimap C++

I have a trouble with adding a new values to a set that is in my map>. When I try to add a value like this: Key key(get<0>(key_args), get<1>(key_args)); if (!(get<0>(key_args) < 1 && get<0>(key_args) >…
Artem
  • 21
  • 5
0
votes
1 answer

emplace and try_emplace with copy constructor

I have an issue with emplace and try_emplace as they always use the copy constructors when moving an object in. #include #include #include using namespace std; class Too { public: Too(int x, int y):x_(x),…
Quang Le
  • 13
  • 1
  • 3
0
votes
2 answers

Emplace method in own deque

How can I write my own emplace method in my deque? For example, I have this emplace_front function template void emplace_front(Args&& ...val) { ??? } and Node constructor template Node(Args&&... val) :…
Ja4
  • 23
  • 4
0
votes
2 answers

Error in implementing emplace_back() binary '=': no operator found which takes a right-hand operand of type 'T *'

Code below, for my linked list implementation and specifically emplace_back below. What am I doing wrong and how to fix? Error I am getting is: Build started... 1>------ Build started: Project: so_list_emplace, Configuration: Debug Win32…
Angus Comber
  • 9,316
  • 14
  • 59
  • 107
0
votes
1 answer

c++17 correct moving resource between unique_ptr in containers

How to correct move resource from existed unique_ptr to another created in container? I want to put to container some unique_ptr with resource from another unique_ptr. Or maybe move one unique_ptr from one container to another container. Some code…
Pasha
  • 89
  • 9
0
votes
0 answers

What is the difference between std::list::insert() and std::list::emplace()

What is the difference between std::list::insert() and std::list::emplace()? I need to pass a value to either of them. I had expected emplace to create an instance of the list's data type without initializing it with some value I pass but just…
Razzupaltuff
  • 2,250
  • 2
  • 21
  • 37
0
votes
1 answer

Building a std::map and issue with using std::emplace

Code: std::map mapHistory; // History list is in ascending date order for (auto& sHistItem : m_listDiscussionItemHist) { if (m_bFullHistoryMode) mapHistory.emplace(sHistItem.strName, sHistItem); else…
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
0
votes
3 answers

Unable to avoid copying while pushing objects with copy-construcor into a vector

I'm trying to avoid copying with emplace_back() and reserve(). But when I've tried to do it, i caught myself getting 3 copies for reason i cannot really understand. reserve() actually helps to avoid copying, but emplace_back() actually does nothing…
Venci
  • 25
  • 3
0
votes
1 answer

Is there ever a time where insert is better than emplace on a map in C++?

To head this off, I've already read through the following question. My question could be seen as a follow up to this. insert vs emplace vs operator[] in c++ map From what info I can find, I don't see any reason why you would ever want to use an…
TheChemist
  • 352
  • 2
  • 12
0
votes
1 answer

Is it still necessary to use std move even if auto && has been used

As we know, STL usually offered two kinds of functions to insert an element: insert/push and emplace. Let's say I want to emplace all of elements from one container to another. for (auto &&element : myMap) { anotherMap.emplace(element); // vs…
Yves
  • 11,597
  • 17
  • 83
  • 180
0
votes
0 answers

std::vector::emplace_back error in class with a custom constructor

My program has a class similar to the following: class IOWorker { std::thread thread_; boost::asio::io_service ios_; boost::optional work_; …
void
  • 338
  • 5
  • 19
0
votes
1 answer

Is emplace only advantageous over push when the object being emplaced is a non-POD and an r-value?

From what I've read regarding emplace vs push for STL containers, the benefit of emplace over push is only evident when the object you're emplacing is an r-value and a non-POD type. Is this the only situation in which emplace is faster?
user5965026
  • 465
  • 5
  • 16
0
votes
3 answers

try_emplace doesn't work (as desired) when you have an inheritance structure for the value type?

// A couple of simple structs that inherit AND have different ctor arguments struct A { A(int) {} }; // Note that "B" is a subclass of "A", which is important below struct B : A { B(int, char) : A(0) // The 0 doesn't matter {} // Note…
user5406764
  • 1,627
  • 2
  • 16
  • 23
0
votes
1 answer

Can I emplace arguments into a class member without first default-constructing said member?

template class InternalObject { public: using RefCountT = unsigned short; template static void* emplace(CtorTs&&...); Types type; RefCountT reference_count; bool is_immovable; …
Doot
  • 555
  • 5
  • 15
0
votes
1 answer

Confusion about the different efficiency between emplacement and insertion for containers not allowing duplicates

In Item 41 from Effective Modern C++, the following is one of the situations that give a chance for emplacement functions to be more performant than the insertion conterparts: The container is unlikely to reject the new value as a duplicate the…
Enlico
  • 23,259
  • 6
  • 48
  • 102