0

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 insert over an emplace. Using emplace seems to be the better choice across the board. It seems obviously better with complex types. but even with pointers and POD, you're still saving on copying the data around a few times. (Edit: Removed to simplify the question)

Am I wrong in anything here? Regardless, is there a use case for insert where it is the better choice? Or more generally (assuming I'm wrong), can someone detail the benefits and drawbacks of each and when you would choose one over the other?

TheChemist
  • 352
  • 2
  • 12

1 Answers1

0

Insert has both const& and && overloads with a fixed type. This can induce conversions earlier and allows for {} based construction.

Emplace does not. So the conversion happens within the method (instead outside of it), and you cannot use {} to construct the type without naming it.

That is about it.


As for your second point (about insert causing extra copies with plain old data):

Copying a integer or whatever has no observable effects in the abstract machine if the intermediate storage doesn't have pointers or references stored to it. C++ is specified on an abstract machine, and only the observable features of that machine matter to what your compiler has to do.

int x = 3;
int y = x;
int z = y;

If nobody ever mentions x or y again, there are no observable effects from their existence, and the above is equivalent to int z = 3;.

Some compilers in some debug settings will create those no-side effect variables. But that is only to help debugging, not what C++ actually states programs are supposed to do.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • I'm not sure I follow the point you're trying to make. Are you just trying to say that using insert with POD has no extra cost due to compiler optims and therefore is equivalent to emplace? – TheChemist Oct 14 '21 at 14:29
  • @TheChemist Part of my answer points out that "but even with pointers and POD, you're still saving on copying the data around a few times." is nonsense outside of debug builds. The rest talks about how insert and emplace work differently. – Yakk - Adam Nevraumont Oct 14 '21 at 14:48
  • I guess I just don't follow then. You've described the low-level without context or example so it hasn't cleared up anything for me. Are you saying that fixed types (e.g. int32_t) have an advantage using insert over emplace because of whatever effect an earlier conversion has? And by {} construction, are you referring to C++11 aggregate initialization? – TheChemist Oct 14 '21 at 16:10
  • @TheChemist I have put a line crossing my answer. The top addresses "what is the advantage of insert". The bottom addresses your error thinking that copying ints around in function calls is a fundamental problem. I am not claiming the advantage is something ground breaking, but I am describing the entire advantage of insert over emplace. – Yakk - Adam Nevraumont Oct 14 '21 at 17:10
  • Sorry, I think I'm not being clear. What I'm trying to say is that I don't understand your answer. What advantage do the const& and && overloads provide and what is a use-case/example of that? Is this purely an advantage in that it makes it easier to write the code or is there some performance gain here? My knowledge of the inner workings of stl containers is pretty limited. Please explain it to me like I've only just learned how to print "Hello World." – TheChemist Oct 14 '21 at 17:16
  • @TheChemist "This can induce conversions earlier and allows for {} based construction." That is it. When it is converted to the stored type can happen before the call, or in the container's code. insert does it "earlier". And `{}` based construction won't work with emplace. These are not deep, complex differences, with deep, complex implications. `s.insert({"hello"})` could comple, `s.emplace({"hello"})` can never compile. – Yakk - Adam Nevraumont Oct 14 '21 at 17:54
  • If I understand you correctly, you're saying the advantage in insert is simply in coding style/simplicity/safety in that it allows you to use aggregate/list initializers? – TheChemist Oct 14 '21 at 19:01