std::make_move_iterator(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… NAME πŸ’…

std::make_move_iterator(3) β€” bestie, welcome to the std::make_move_iterator page from the C++ Standard Libary manual, and it's literally here to hand you the move-iterator glow-up ✨


πŸ¦‹ Synopsis πŸ¦‹

okay so first things first: it's defined in header <iterator> 🫢

Defined in header <iterator>
template< class Iter >
(since C++11)
std::move_iterator<Iter> make_move_iterator( Iter i );
(until C++17)
template< class Iter >
(since C++17)
constexpr std::move_iterator<Iter> make_move_iterator( Iter i );

make_move_iterator is a convenience function template that constructs a std::move_iterator for the given iterator i with the type deduced from the type of the argument. no cap, that's the whole serve πŸ’…

πŸŽ€ Parameters πŸŽ€

  • i β€” input iterator to be converted to move iterator

🌸 Return value 🌸

A std::move_iterator which can be used to move from the elements accessed through i. she's giving access-with-a-move-vibe, periodt.

✨ Possible implementation ✨

here's the possible-implementation mood board, and yes the code is exact:

template<class Iter>
constexpr std::move_iterator<Iter> make_move_iterator(Iter i)
{
    return std::move_iterator<Iter>(std::move(i));
}

πŸ’– Example πŸ’–

// Run this code πŸ‘ˆ that's what the page says right before the code, so we're keeping it.

#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <string>
#include <vector>

auto print = [](auto const rem, auto const& seq)
{
    for (std::cout << rem; auto const& str : seq)
        std::cout << std::quoted(str) << ' ';
    std::cout << '\n';
};

int main()
{
    std::list<std::string> s{"one", "two", "three"};

    std::vector<std::string> v1(s.begin(), s.end()); // copy

    std::vector<std::string> v2(std::make_move_iterator(s.begin()),
                                std::make_move_iterator(s.end())); // move

    print("v1 now holds: ", v1);
    print("v2 now holds: ", v2);
    print("original list now holds: ", s);
}

🌈 Possible output 🌈

v1 now holds: "one" "two" "three"
v2 now holds: "one" "two" "three"
original list now holds: "" "" ""

🫢 Defect reports 🫢

okay so the following behavior-changing defect reports were applied retroactively to previously published C++ standards:

  • LWG 2061 β€” applied to C++11. behavior as published: make_move_iterator did not convert array arguments to pointers. correct behavior: made to convert.

πŸ‘‘ See also πŸ‘‘

  • move_iterator β€” iterator adaptor which dereferences to an rvalue; since C++11, class template.
  • move β€” obtains an rvalue reference; since C++11, function template.

source: http://cppreference.com Β· version/date: 2024.06.10 β€” mwah, bestie πŸ’‹

πŸ“¦ where this page comes from