This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

insert vs emplace


Hello.

I have been pondering the attached program for a while and can't figure
out if it invokes undefined behaviour or what the proper output of it
should be so I am hoping someone here could help me finding the relevant
parts of the c++ standard.

/MF
#include <iostream>
#include <vector>
int main()
{
	{
		std::vector<int> v { 1, 2, 3 };
		v.reserve(4);
		v.insert(v.begin(), v[2]);
		std::cout << v[0] << ' ' << v[1] << ' ' << v[2] << ' ' << v[3] << '\n';
	}
	{
		std::vector<int> v { 1, 2, 3 };
		v.reserve(4);
		v.emplace(v.begin(), v[2]);
		std::cout << v[0] << ' ' << v[1] << ' ' << v[2] << ' ' << v[3] << '\n';
	}
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]