I am trying to forward some arguments to do inplace construction of objects . I don't quite get the rationale behind the usage of emplace in associative containers or may be I am just using/thinking in a wrong way. It would be great if someone can share code snippets for usage.
Associative container like map always store an object of kind pair() , and the emplace function says that it will call the constructor of the object stored (which for is always pair in case of maps) by forwarding the arguments. So are we just restricted to provide two arguments (key , value) even if the the function has variadic signature ?
When I used emplace with boost containers before I could pass arguments like : emplace(arg1, arg2,arg3,arg4) // where arg2,arg3,arg4 were used for constructing the object and arg 1 was key.
when compiling with new gcc-4.6 and c++11 , this breaks But now I have to do something like : emplace (arg1 , myobj(arg2,arg3,arg4)); // to make the same code work ;
So the new emplace doesn't do any piece wise construction like boost ? And am I restricted to provide only 2 arguments to maps , because pairs will always accept two argument for their constructors.
emplace(key, arg1, arg2, arg3)) should work. - Some programmer dudestd::pairsupports piecewise construction, although it doesn't look quite so appealing. - Cubbi