Thursday 28 June 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...