c++ - Why would I ever use push_back instead of emplace_back? -
c++11 vectors have new function emplace_back. unlike push_back, relies on compiler optimizations avoid copies, emplace_back uses perfect forwarding send arguments directly constructor create object in-place. seems me emplace_back push_back can do, of time better (but never worse).
what reason have use push_back?
push_back allows use of uniform initialization, i'm fond of. instance:
struct aggregate { int foo; int bar; }; std::vector<aggregate> v; v.push_back({ 42, 121 }); on other hand, v.emplace_back({ 42, 121 }); not work.
Comments
Post a Comment