According to this, gcc-4.7
does not fully support emplace()
.
gcc-4.8
would have fully support of emplace()
, but would require a little differnt syntax:
table.emplace(std::piecewise_construct, std::forward_as_tuple(myKey), std::forward_as_tuple(arg1, arg2, arg3));
instead of
table.emplace(myKey, arg1, arg2, arg3); // Does not work.
For forward
to take effect for rvalue, one could use std::move()
to wrap around the arguments.
So it will directly contruct both key and value in place in the container.
std::piecewise_construct
is an argument for special usage of std::pair
constructor. It will ask the constructor to forward the arguments individually to underlying constructor.
This is actually an issue of emplace()
for associative containers. The original intention is to use the first argument for key, and the following arguments for value, like what is described in the question. See this for further details on this issue.
Thanks to Zeta, for pointing out forward
issue.