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
4
votes
5 answers

Insert an object pointer into a map of maps through emplace() does not work

I'm trying to insert a pointer object to a map through emplace() but it does not work. I've created a simple representation of the problem below. I'm trying to insert to newFooList pointer object type Foo*. I can't seem to find a way to create a…
Inian
  • 80,270
  • 14
  • 142
  • 161
4
votes
1 answer

how to emplace_back(pair) efficiently?

I have using namespace std; // for convenience in SO question only vector, int>> foo; and want to emplace_back an element with pair::first holding {i,j,k} and pair::second holding q. The only way I could get this compiling was…
Walter
  • 44,150
  • 20
  • 113
  • 196
4
votes
2 answers

Is there a difference between std::stack::push() and std::stack::emplace() in this case?

Look at this code: struct Dummy { int bla; int blabla; char character; Dummy (int b, int bb, char c) : bla(b), blabla(bb), character(c) {} }; std::stack s; Dummy dummy; s.push(dummy); // (1) s.emplace(dummy); …
NPS
  • 6,003
  • 11
  • 53
  • 90
4
votes
1 answer

Emplace a pointer to a multimap of shared_ptr's doesn't work

Vector works properly Header std::vector> subnodes_m; Definition void CompositeSceneNode::AddChild(SceneNode* subnode_p) { subnodes_m.emplace_back(subnode_p); } Multimap doesn't Header std::multimap
user2032932
  • 197
  • 2
  • 11
4
votes
1 answer

std::map emplace gcc 4.8.2

I am trying to use the emplace function of std::map, but it seems it is not implemented (but I read it was implemented in 4.8) The following code: std::map maps; maps.emplace("Test", 1.0); leads to: class…
Thorsten
  • 333
  • 1
  • 5
  • 20
4
votes
3 answers

Efficiently and elegantly returning emplaced unique_ptr

I found out (thanks to a StackOverflow comment) a security flaw in my code: std::vector> items; template Item& create(TS&&... mArgs) { auto item(new Item(std::forward(mArgs)...); …
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
3
votes
1 answer

Copy/move elision vs emplace with std::optional

I was using an std::optional to store a struct, and I initially went with: std::optional optPosition; // declared somewhere as a class member optPosition.emplace(..., ..., ...); works great, but I don't really like that you have to define…
fab
  • 55
  • 4
3
votes
2 answers

Replace a variable of a type with const members

Suppose I have a class with some constant member: class MyClass { public: MyClass(int a) : a(a) { } MyClass() : MyClass(0) { } ~MyClass() { } const int a; }; Now I want to store an instance of MyClass somewhere, e.g. as a…
Samufi
  • 2,465
  • 3
  • 19
  • 43
3
votes
1 answer

Does emplace_hint on std::multimap preserves the relative ordering of equivalent elements?

According to http://www.cplusplus.com/reference/map/multimap/emplace_hint/ The relative ordering of equivalent elements is preserved, and newly inserted elements follow their equivalents already in the container. The value in position is used as…
3
votes
1 answer

Can I in-place construct in Rust?

I like emplace()ing in C++ which allows to save on move construction and destruction: e.g. strings.push_back("abcd"s) means construct, move, destruct the temporary while strings.emplace_back("abcd") is just "construct". Can anything similar be…
passing_through
  • 1,778
  • 12
  • 24
3
votes
2 answers

weak_ptr, shared_ptr in lambda capture list and std::queue::emplace

I have a function which enqueue lambda using std::queue's build-in emplace. I created shared_ptr object (task), which later I would capture in lambda. template auto submit(Func&& f, Args&&... args) { …
VanDyke
  • 31
  • 1
  • 3
3
votes
1 answer

How best to use emplace with std::map

I was experimenting with using std::map::emplace() instead of insert. I have a simple test program below: #include #include #include class CTest { public: CTest() : Value(0), Name() { std::cout << "Default constructor"…
jignatius
  • 6,304
  • 2
  • 15
  • 30
3
votes
1 answer

How does std::set and std::unordered_set construct elements in place with emplace()?

The documentation for both containers say that emplace() function constructs elements in place, but how do they know the location of the new element before the element is constructed? For example, unordered_set places elements according to their…
Sweeney Todd
  • 880
  • 1
  • 11
  • 25
3
votes
1 answer

construct std::pair in-place in vector::emplace_back

I have a class A defined like bellow: class A { public: A() = default; explicit A(uint32_t a, uint32_t b) { std::cout << "construct" << std::endl; } A(const A& obj) { std::cout << "copy" << std::endl; *this =…
Cricket
  • 491
  • 1
  • 7
  • 16
3
votes
1 answer

Emplacement-like construction for std::vector

Imagine I want to construct a fixed-size std::vector of objects without move or copy constructors, such as std::atomic. In this case the underlying std::atomic class has a 1-arg constructor which takes an int, as well as a default constructor…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386