std::move_i...:operator[](3)

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

πŸ’… std::move_iterator::operator[] β€” bestie, it’s indexing but make it move ✨

Okay so this is the std::move_i...:operator[](3) page from the C++ Standard Libary. It’s literally all about std::move_iterator::operator[], and it’s giving β€œreturn a reference to the element at a specified relative location” energy. 🫢

🌸 NAME 🌸

std::move_iterator::operator[] β€” yes, the page title repeats itself: std::move_iterator::operator[]. Iconic, honestly. πŸ’…

✨ Synopsis ✨

Here’s the exact signature tea, with all the since/until eras kept intact:

/* unspecified */ operator[]( difference_type n ) const;
(since C++11)
(until C++17)

constexpr /* unspecified */ operator[]( difference_type n ) const;
(since C++17)
(until C++20)

constexpr reference operator[]( difference_type n ) const;
(since C++20)

πŸŽ€ Parameters πŸŽ€

  • n β€” position relative to current location.

πŸ’– Return value πŸ’–

An rvalue reference to the element at that relative location. Like, literally:

  • until C++20: std::move(base()[n])
  • since C++20: ranges::iter_move(base() + n)

🧚 Notes 🧚

Real talk: the return type is unspecified because the return type of the underlying iterator’s operator[] is also unspecified β€” see LegacyRandomAccessIterator. The (until C++20) label is part of this note, bestie, don’t @ me. 😭

πŸ“ Example πŸ“

// Run this code

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

void print(auto rem, auto const& v)
{
    for (std::cout << rem; auto const& e : v)
        std::cout << std::quoted(e) << ' ';
    std::cout << '\n';
}

int main()
{
    std::vector<std::string> p{"alpha", "beta", "gamma", "delta"}, q;
    print("1) p: ", p);

    std::move_iterator it{p.begin()};

    for (std::size_t t{}; t != p.size(); ++t)
        q.emplace_back(it[t]);

    print("2) p: ", p);
    print("3) q: ", q);

    std::list l{1, 2, 3};
    std::move_iterator it2{l.begin()};
//  it2[1] = 13; // compilation error: the underlying iterator
                 // does not model the random access iterator
//  *it2 = 999;  // compilation error: using rvalue as lvalue
}

🌈 Possible output 🌈

1) p: "alpha" "beta" "gamma" "delta"
2) p: "" "" "" ""
3) q: "alpha" "beta" "gamma" "delta"

πŸ¦‹ See also πŸ¦‹

The see-also table references operator* and operator-> β€” operator-> accesses the pointed-to element. It also carries these exact labels: (C++11), (public member function), and (C++11)(deprecated in C++20). Nothing dropped, bestie. πŸ’œ


That’s the whole page, bestie. Footer source/version/date energy: http://cppreference.com Β· 2024.06.10 Β· std::move_i...:operator[](3) β€” mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from