Copy elision when creating object inside emplace()

后端 未结 4 975
无人共我
无人共我 2021-02-12 19:00

I see a lot of code at work where people use emplace and emplace_back with a temporary object, like this:

struct A {
    A::A(int, int);
};

vector v;
v         


        
4条回答
  •  情话喂你
    2021-02-12 19:55

    I just want to add

    There is a great 5 minutes lightning talk about copy elision and RVO from Jon Kalb https://youtu.be/fSB57PiXpRw

    Also, you might get different results using different compilers gcc, clang or icc

    See compiler explorer, try different compilers and settings and see for yourself

    https://godbolt.org/g/Yjo9qA

提交回复
热议问题