std::move with std::make_pair

纵然是瞬间 提交于 2019-12-05 23:07:36

问题


Is there any difference between:

std::map <int,std::pair<T,T>> m;
T t1,t2;
m.emplace(1,std::make_pair(t1,t2));

and:

std::map <int,std::pair<T,T>> m;
T t1,t2;
m.emplace(1,std::move(std::make_pair(t1,t2)));

Is the std::move redundant here? Will std::map::emplace and perfect forwarding take care of allocating the std::pair directly in the std::map?


回答1:


std::make_pair(...) and std::move(std::make_pair(...)) are both rvalue expressions (the first one is a prvalue, the second one is an xvalue). Since emplace takes forwarding references, both are deduced as the same type, so std::move in this case is redundant, but in a general case, a redundant std::move can inhibit copy-elision.

m.emplace(1, std::make_pair(t1, t2));

is equivalent to:

auto&& arg = std::make_pair(t1, t2);
std::pair<const int, std::pair<T, T>> e(1, std::forward<std::pair<T, T>>(arg));

which performs the following initialization of the map element's value:

auto&& arg = std::make_pair(t1, t2);
std::pair<T, T> p(std::forward<std::pair<T, T>>(arg));

Note that this is different from:

std::pair<T, T> p(t1, t2);

The former first creates a prvalue pair (makes copies of t1 and t2), which is then moved from (moves both the copied t1 and t2 into p). No copy-elision takes place.

The latter uses t1 and t2 to initialize both Ts stored in the pair.

To avoid the unnecessary move resulting from the first syntax, you can instead utilize piecewise construction:

m.emplace(std::piecewise_construct
        , std::forward_as_tuple(1)
        , std::forward_as_tuple(t1, t2));

that will be equivalent to:

auto&& arg = std::tuple<T&, T&>(t1, t2);
std::pair<T, T> p(std::get<0>(std::forward<std::tuple<T&, T&>>(arg))
                , std::get<1>(std::forward<std::tuple<T&, T&>>(arg)));

that will initialize the elements of the pair from reference members bound to original t1 and t2.




回答2:


m.emplace(std::make_pair(1, std::make_pair(t1,t2)));

will call move constructor.



来源:https://stackoverflow.com/questions/36906575/stdmove-with-stdmake-pair

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