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
6
votes
0 answers

Implementation of push_back via emplace_back

Suppose we want design a container C similar to std::vector. Is it a good idea to implement push_back by calling emplace_back, such as: template class C { public: ... template void emplace_back(Args&&...…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
6
votes
1 answer

Exception in std::vector<>::emplace_back() safe?

What happens when an exception is thrown in std::vector<>::emplace_back()? For example: class Foo { public: Foo(int bar) { if (bar == 4) throw std::exception("Something went wrong"); } } and std::vector>…
Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105
6
votes
2 answers

std::unordered_map::emplace object creation

I was in the process of selecting one of two methods of putting things into an unordered_map: std::unordered_map map; map.emplace( std::piecewise_construct, std::forward_as_tuple(a), std::forward_as_tuple(b, c,…
vmpstr
  • 5,051
  • 2
  • 25
  • 25
6
votes
1 answer

For a map of objects, can I emplace objects, or just pairs?

I'm using C++ 11 and gcc-4.7.0. I'm looking for an STL solution. I'd like to have an unsorted multimap that contains objects of myClass with short strings as keys. Emplace looks like a good way to put the objects into the map as I construct them,…
Qaz
  • 1,556
  • 2
  • 20
  • 34
5
votes
1 answer

Cannot emplace_back() a braced initializer on a vector of vectors

This is somewhat related to a previous question I asked regarding using emplace_back on a vector of pairs. emplace_back() vs push_back when inserting a pair into std::vector Now my question pertains to using emplace_back on a vector of vectors. Here…
24n8
  • 1,898
  • 1
  • 12
  • 25
5
votes
2 answers

emplace and default constructors

Given the following code, I was surprised that try_emplace could not work out to use the default constructor demonstrated in the first line of the main function, instead complaining there is no matching function call to Element::Element(double,…
Madden
  • 1,024
  • 1
  • 9
  • 27
5
votes
2 answers

g++ 4.9.3 complains that friended ctor is private with .emplace_back(), but likes .push_back()

I must be missing one of the finer points regarding emplace() and friends. Here's a complete, minimal example that reproduces the problem with g++ 4.9.3: class Foo { public: class Bar { private: friend class Foo; Bar(Foo…
Steger
  • 887
  • 7
  • 16
5
votes
1 answer

Emplace directly in std::map of pair

Why this code won't compile? std::map> m; m.emplace(1,1,1); Assuming that we can edit the code of std::map::emplace, is it possible to alter it in order to make previous code valid?
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
5
votes
2 answers

C++ Set emplace vs insert when an object is already created

class TestClass { public: TestClass(string s) { } }; When there is TestClass, I understand the difference between emplace and insert (emplace constructs in place while insert copies) set test_set; …
MaxHeap
  • 1,138
  • 2
  • 11
  • 20
5
votes
4 answers

emplace unordered_set in unordered_map

How can I add a (statically defined) unordered_set to an unordered_map, without having to copy the unordered_set? I tried this: std::unordered_map> my_map; for (int i=0; i<100; i++) my_map.emplace(i, {"foo",…
Valentin Lorentz
  • 9,556
  • 6
  • 47
  • 69
5
votes
1 answer

How to use emplace() in a std::map whose value is a std::set (map from something to a set)?

The Problem I have a std::map> named misi. I'm wondering why misi.emplace(2345, {6, 9}); and misi.emplace({2345, {6, 9}}); don't work as expected, as shown below. The Code #include // std:set #include
5
votes
2 answers

How does set::emplace handle objects that are already in a set?

I have a set of objects, and I want to use emplace to add objects to the set. If an equivalent object does not already exist in the set, set::emplace creates an object and puts it in the set. If the set already has an equivalent object, set::emplace…
Qaz
  • 1,556
  • 2
  • 20
  • 34
5
votes
1 answer

RVO, move semantics and the struggle towards optimal code

If I get it correctly, move semantics allows to move and reuse resources from temporary, unnamed objects. RVO, albeit preceding move semantics goes further and "steals" the entire object to avoid the extra constructor call and assignment/copy…
user2341104
4
votes
1 answer

How to emplace to a std::vector of std::array?

With a vector of vectors, I can do this: std::vector> vov; vov.emplace_back(std::initializer_list{0, 0}); However, the equivalent fails for the vector of std::array: std::vector>…
Fido
  • 320
  • 1
  • 2
  • 15
4
votes
2 answers

Why is there no emplace or emplace_back for std::string?

I'm pretty sure I've seen this asked on here before, but I can't seem to find the post when I tried searching, and I don't recall the answer. Why is there no emplace or emplace_back for std::string? I don't think it's related to the use of char,…
24n8
  • 1,898
  • 1
  • 12
  • 25
1 2
3
12 13