Why emplace_back is faster than push_back?

后端 未结 1 1939
太阳男子
太阳男子 2020-12-04 09:59

I thought that emplace_back would be the winner, when doing something like this:

v.push_back(myClass(arg1, arg2));

because

相关标签:
1条回答
  • 2020-12-04 10:28

    Your test case isn't very helpful. push_back takes a container element and copies/moves it into the container. emplace_back takes arbitrary arguments and constructs from those a new container element. But if you pass a single argument that's already of element type to emplace_back, you'll just use the copy/move constructor anyway.

    Here's a better comparison:

    Foo x; Bar y; Zip z;
    
    v.push_back(T(x, y, z));  // make temporary, push it back
    v.emplace_back(x, y, z);  // no temporary, directly construct T(x, y, z) in place
    

    The key difference, however, is that emplace_back performs explicit conversions:

    std::vector<std::unique_ptr<Foo>> v;
    v.emplace_back(new Foo(1, 'x', true));  // constructor is explicit!
    

    This example will be mildly contrived in the future, when you should say v.push_back(std::make_unique<Foo>(1, 'x', true)). However, other constructions are very nice with emplace, too:

    std::vector<std::thread> threads;
    threads.emplace_back(do_work, 10, "foo");    // call do_work(10, "foo")
    threads.emplace_back(&Foo::g, x, 20, false);  // call x.g(20, false)
    
    0 讨论(0)
提交回复
热议问题