I was just wondering if there's a difference between the following two lines:
objStudents.push_back({ "George", 1 });
objStudents.push_back(Student("Jack", 10));
Is one way more efficient than the other? Or is it just a different syntax that completes the same task?
Assumming
Studenthas a constructor with the right arguments, the difference is that whileobjStudents.push_back(Student("Jack", 10));always compiles,objStudents.push_back({ "George", 1 });only compiles if the constructor is not declaredexplicit. On the other hand, ifStudentis an aggregate (e.g.struct Student { string s; int i; };), then onlyobjStudents.push_back({ "George", 1 });compiles.Both forms are equally efficient if they compile - both construct
Studentfrom e.g."George", 1followed by invoking move (if exists) or copy constructor.In the case with a constructor, a more efficient form, which compiles even if the constructor is
explicit, isobjStudents.emplace_back("George", 1);, which constructsStudentdirectly inside the vector and avoids the extra move or copy. Of course, "more efficient" here is theoretical - a compiler may be able to optimize away the extra operation in the form withpush_back.