push_back vs emplace_back: what's the difference and which to choose?
Greetings, colleagues! I'm a beginner C++ developer and recently encountered a dilemma: when to use push_back and when to use emplace_back? \n\nI have a simple class:\n\n```cpp\nclass MyClass {\npublic:\n MyClass(int x, int y) : x_(x), y_(y) {}\nprivate:\n int x_, y_;\n};\n```\n\nI add objects to a vector in two ways:\n\n```cpp\nstd::vector vec;\n\n// Method 1\nvec.push_back(MyClass(10, 20));\n\n// Method 2\nvec.emplace_back(10, 20);\n```\n\nBoth work, but I've heard that emplace_back is faster. How critical is this? Are there cases where push_back is preferable? Or is emplace_back always better?\n\nI'm especially interested in: what happens with temporary objects, move semantics, and copying? There's a lot of contradictory information on the internet, I'd like to get a clear answer.