Thursday, June 28, 2018

c++ - Difference between push_back and emplace_back in the case of a temporary value

Consider this snippet:




#include 

struct A
{
A(int b): m_b(b) { }

private:
int m_b;
};


int main()
{
std::vector vec;

vec.push_back( A(3) );

vec.emplace_back( 3 );
}



In that particular case where a temporary value is passed, is there any difference between the 2 calls that a compiler won’t optimize away? Should I prefer one to the other?

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...