Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 904
user179156 Avatar asked Dec 08 '22 19:12

user179156


1 Answers

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<A1>(arg1),
          std::forward_as_tuple<A2,A3,A4>(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)

like image 185
Jonathan Wakely Avatar answered Dec 11 '22 08:12

Jonathan Wakely