Wednesday 3 February 2016

c++ - use of emplace(args&& ...) in associative containers




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.


Answer




So the new emplace doesn't do any piece wise construction like boost ?




What you refer to a "piece wise construction" is not what the standard refers to as piecewise construction, which is:



m.emplace(std::piecewise_construct,

std::forward_as_tuple(arg1),
std::forward_as_tuple(arg2, arg3, arg4));


This does exactly what you want, forwarding the tuples of args to the first and second pair members (but be aware that with GCC 4.6 this requires an accessible copy constructor for each argument type, see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51183 -- this requirement is fixed for GCC 4.7 by using delegating constructors, not supported by GCC 4.6)


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...