use of emplace(args&& …) in associative containers

风格不统一 提交于 2019-12-21 18:02:15

问题


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.


回答1:


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)




回答2:


This is indeed a defect in the standard, it is addressed at length in N3178.

To quote,

the only way to construct an object of value_type is to supply exactly two arguments for Key and Value, a pair, or a piecewise_construct_t followed by two tuples. The original emplace() proposal would have allowed you to specify a Key value followed by any number of constructor arguments for Value. When we removed the variadic constructor to pair, this ability went away

...

the status quo is to use piecewise_construct_t if you want to construct an object.

It was closed as "NAD"



来源:https://stackoverflow.com/questions/13689482/use-of-emplaceargs-in-associative-containers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!