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

How to avoid temporary copies when using emplace to add to a std::map?

#include #include using namespace std; struct FooStruct { int a; int b; }; int main() { map fooMap; fooMap.emplace(0, {1, 2}); return 0; } In terms of preventing temporary…
DigitalEye
  • 1,456
  • 3
  • 17
  • 26
3
votes
1 answer

std::vector emplace and std::vector emplace back with pair

I have this code: std::vector> vec; vec.emplace_back("a", 1); //success vec.emplace(vec.end(), "b", 2); //compile error vec.emplace_back(std::make_pair("c", 3)); …
Phạm Văn Thông
  • 743
  • 2
  • 7
  • 21
3
votes
1 answer

Why is the move constructor only called when there is already an element in the vector?

I am trying to learn the new features in C++11. And I am testing the following code in XCode. #include #include #include class CClass { std::string s; public: CClass() { std::cout<<"Default…
3
votes
3 answers

Editing the value in an unordered map for a given key

The following is C++ code to get a count of the words in magazine. I'm trying to add the word if its value does not exist and if it does, increment it. unordered_maphash; vector magazine(m); for(int i = 0;i >…
Bharg
  • 311
  • 1
  • 2
  • 15
3
votes
1 answer

How Can I avoid implicit move constructor inside a lambda function

I am using "emplace" method to avoid memory copy. But, when I am using the "emplace" inside a Lambda function. It always call implicit move constructor. How I can avoid memory copy inside a Lambda function? This sample program should not print “I am…
3
votes
1 answer

emplace and unordered_map>

I have a std::unordered_map>. What is the syntax of emplaceing a value into the map? unordered_map> contig_sizes; string key{"key"}; array value{1, 2}; // OK ---1 contig_sizes.emplace(key,…
Leo Goodstadt
  • 2,519
  • 1
  • 23
  • 23
3
votes
1 answer

Why doesn't boost::lockfree::spsc_queue have emplace?

The regular std::vector has emplace_back which avoid an unnecessary copy. Is there a reason spsc_queue doesn't support this? Is it impossible to do emplace with lock-free queues for some reason?
Thomas Johnson
  • 10,776
  • 18
  • 60
  • 98
3
votes
3 answers

How can a default-constructed object of a non-copyable class be emplaced into a boost::container::map?

Consider the folllowing C++03 code (I have to make the code compatible with pre-C++11 compilers): // This class belongs to a third-party SDK and cannot be touched class A { public: explicit A(); explicit A(bool b); private: //…
Doodler
  • 167
  • 1
  • 7
3
votes
1 answer

Emplacement in D

Does D have something similar to C++11's emplace_back()? I typically want this when I append a single or multiple structs to the end of an array or some other D container with value semantics on its members? Update: I guess this is the answer…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
2
votes
1 answer

emplace_back with initializer list

I'm trying to run the following code on VS2022: #include #include int main() { std::vector> vec; //auto list = (std::initializer_list){ 5, 6 }; …
2
votes
1 answer

Emplacing to the back of a forward_list segfaults

This must be a stupid oversight on my part, but how can I add to the back of a std::forward_list? I know usually the forward_list implementation doesn't contain an iterator to the back, but according to cppreference the standard forward_list is…
glades
  • 3,778
  • 1
  • 12
  • 34
2
votes
1 answer

How to make map.emplace automatically choose constructor in c++17?

In c++17 first, I have a struct like typedef struct ST_A{ int a; string s; ST_A(const int a, const string&s) :a(a),s(s) {} }st_A; And Now I have a simple map and emplace twice in terms of cppreference map
f1msch
  • 509
  • 2
  • 12
2
votes
1 answer

nested vector with polymorphic_allocator cannot be constructed via emplace_back

The following code: std::pmr::vector> outer_vec(std::pmr::get_default_resource()); outer_vec.emplace_back(std::pmr::get_default_resource()); fails with quite inconceivable error message ending with static assertion failed:…
Zereges
  • 5,139
  • 1
  • 25
  • 49
2
votes
2 answers

How does std::map's emplace() avoid premature construction?

I am confused about std::maps's implementation of emplace(). emplace() is a variadic template function with the following declaration: template iterator emplace( Args&&... args ); As far as I understand, emplace() completely avoids…
2
votes
2 answers

C++: insert element into std::map where MyStruct can only be aggregate initialized and contains const unique pointers

Here is what I am trying to do and what I have tried: #include #include #include using namespace std; struct MyStruct { const unique_ptr a; const unique_ptr b; }; int main() { auto a =…
Mircode
  • 432
  • 5
  • 12