Std :: move with std :: make_pair

Is there a 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 it std::moveredundant here? Will std::map::emplaceand perfect forwardingtake care of the allocation std::pairdirectly in std::map?

+4
source share
2 answers

std::make_pair(...)and std::move(std::make_pair(...))- both rvalue expressions (the first is prvalue, the second is xvalue). Since it emplaceforwards links, both are inferred as the same type, therefore, std::movein this case it is redundant, but in the general case the redundant std::movecan block copying.

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 value of a map element:

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

Please note that this is different from:

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

prvalue ( t1 t2), ( t1 t2 p). .

t1 t2 T, .

, :

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

:

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)));

, t1 t2.

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

move.

0

All Articles