Get the latest tech news

Don't blindly prefer `emplace_back` to `push_back`


In one of my recent training courses, a student informed me that both clang-tidy and PVS-Studio were complaining about some code of the form

In one of my recent training courses, a student informed me that both clang-tidy and PVS-Studio were complaining about some code of the form Both tools flagged this line as “bad style.” clang-tidy even offered a (SARCASM ALERT) helpful fixit: emplace_back, on the other hand, is a dumb perfect-forwarding template: it doesn’t know that the relevant constructor overload will end up being string(const char *) in each case.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of C++

C++

Photo of emplace_back

emplace_back

Related news:

News photo

Safer code in C++ with lifetime bounds

News photo

Scan HTML even faster with SIMD instructions (C++ and C#) – Daniel Lemire's blog

News photo

rr – record and replay debugger for C/C++