Scott Meyers - The evolving search for effective C++ - Keynote @ Meeting C++ 2014

Meeting Cpp · 62K views · 2014 · 1:52:25

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware performance & optimizationtalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about whether emplace_back or emplace is really faster than push_back or insert, since Meyers explains the edge cases in detail.

Recommended by 3 people across 3 different months, 2014 to 2021.

What it is

Scott Meyers' keynote from Meeting C++ 2014 on how advice for writing effective C++ keeps changing as the language evolves. Much of the talk is a deep dive into emplacement versus insertion in standard containers, including the cases where emplace_back and map::emplace perform worse than expected.

What people said

Recommends Massimiliano · 2014-12 · Stack Overflow · on “Difference in std::vector::emplace_back between GCC and VC++”

Regarding the first paragraph of your question, Scott Meyers just recently gave a talk with a deep dive into that specific guideline, worth watching: [this video] (he also refers to Howard Hinnant's benchmark, mentioned in the previous comments)

Recommends mkaes · 2015-03 · Stack Overflow · on “Building a std::map with a std::string parameter and an non-movable/copyable parameter”

Emplacement on map is a rather strange thing. I would recommend Scott Meyers latest book or look at the talk [this video] . It is about cwuks(term by Scott). But to fix your problem. Use the heap. By changing your logger_instance to be a unique_ptr in the manager. E.g: class logger_manager { std::map < std::string, unique_ptr<logger_instance> > loggers; public: void add_logger(std::string logger) { auto it = loggers.find(logger); if (it == loggers.end()) { loggers.emplace(logger, make_unique<logger_instance>(logger)); } } logger_instance& operator[](std::string logger) { return *loggers[logger]; } };

Recommends Elenchev · 2021-02 · Stack Overflow · on “Examples where std::vector::emplace_back is slower than std::vector::push_back?”

Essentially this boils down to std implementations. Theoretically, emplace should always be as fast or faster, except the reality is that no standard library implementation takes full advantage of that. He gave a talk on this exact issue a few years ago: [this video] Check out the first 1 hour of the talk for a more detailed explanation. The Q&A at the end of the talk is relevant as well.

Cites Thomas Eding · 2015-11 · Stack Overflow · on “In STL maps, is it better to use map::insert than []?”

@einpoklum: That is the case when emplacing into newly constructed memory. But due to some standards requirements for map, there are technical reasons why emplace can be slower than insert. The talk is freely available on youtube, such as this link [this video] @ ~38-40 min mark. For an SO link, here's [LINK]

Cites Jts · 2016-02 · Stack Overflow · on “Passing pointer of class member (Map) as default argument to another class method”

Theres an issue with map::emplace(). Basically, emplace needs to construct the object before checking if the key already exists in the map, If it doesn't exist, everything is good. However, if the key exists already, then it will destroy the object (so a temp object was created for nothing). So I'd say no, it's not always better.. Unless you can guaranty that the key does not exist. Then yeah it should be. But just saying emplace is better is not the right answer. Somewhere in the video [this video] Scott Meyers explains it.

Cites sandthorn · 2018-09 · Stack Overflow · on “What's the most efficient way to erase duplicates and sort a vector?”

Ah, that reminds me of one of Scott Meyer's talks about the `CWUK` scenerio [this video] that has a nature of propablities to slow down the `emplace` kind of construction.