iter_swap(s...on_iterator)(3)

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

πŸ’– NAME πŸ’–

Hey bestie! This is the iter_swap(std::common_iterator) page from the C++ Standard Libary manual, and the name is literally iter_swap(std::common_iterator) β€” iter_swap(std::common_iterator). Consistent queen behavior, honestly πŸ’…βœ¨

🌸 SYNOPSIS 🌸

template< std::indirectly_swappable<I> I2, class S2 >

friend constexpr void
    iter_swap( const common_iterator& x,
               const std::common_iterator<I2, S2>& y ) noexcept(/*see below*/);

This one is (since C++20) πŸŽ€

Swaps the objects pointed to by two underlying iterators. Swaps the objects pointed to by two underlying iterators. Yes, that's the whole vibe of the operation.

⚠️ The behavior is undefined if x does not hold an I object or y does not hold an I2 object (i.e. at least one of x and y does not hold an iterator). Like, please do not test fate with this one 😭

The function body is equivalent to ranges::iter_swap(std::get<I>(x.var), std::get<I2>(y.var)).

Important lookup tea: this function template is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::common_iterator<I,S> is an associated class of the arguments. iykyk 🫢

πŸŽ€ PARAMETERS πŸŽ€

  • x, y β€” the iterators to the elements to swap

πŸ’« RETURN VALUE πŸ’«

(none)

⚑ COMPLEXITY ⚑

Constant.

πŸ›‘οΈ EXCEPTIONS πŸ›‘οΈ

noexcept specification:

noexcept(noexcept(ranges::iter_swap(std::declval<const I&>(), std::declval<const I2&>())))

🧚 EXAMPLE 🧚

// Run this code

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> v1{"1", "2", "3", "4", "5"},
                             v2{"Ξ±", "Ξ²", "Ξ³", "Ξ΄", "Ξ΅"};

    using CI = std::common_iterator<
                   std::counted_iterator<std::vector<std::string>::iterator>,
                   std::default_sentinel_t
                   >;

    CI first1{std::counted_iterator{v1.begin(), 3}};
    CI first2{std::counted_iterator{v2.begin(), 4}};
    CI last{std::default_sentinel};

    auto print = [&](auto rem)
    {
        std::cout << rem << "v1 = ";
        std::ranges::copy(v1,
std::ostream_iterator<std::string>{std::cout, " "});
        std::cout << "\nv2 = ";
        std::ranges::copy(v2,
std::ostream_iterator<std::string>{std::cout, " "});
        std::cout << '\n';
    };

    print("Before iter_swap:\n");

    for (; first1 != last && first2 != last; ++first1, ++first2)
        iter_swap(first1, first2); // ADL

    print("After iter_swap:\n");
}

Output

Before iter_swap:
v1 = 1 2 3 4 5
v2 = Ξ± Ξ² Ξ³ Ξ΄ Ξ΅
After iter_swap:
v1 = Ξ± Ξ² Ξ³ 4 5
v2 = 1 2 3 Ξ΄ Ξ΅

🩹 DEFECT REPORTS 🩹

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • LWG 3574 β€” applied to C++20. Behavior as published: variant was fully constexpr (P2231R1) but common_iterator was not. Correct behavior: also made constexpr.

πŸ‘€ SEE ALSO πŸ‘€

  • swap β€” swaps the values of two objects (function template)
  • swap_ranges β€” swaps two ranges of elements (function template)
  • iter_swap β€” swaps the elements pointed to by two iterators (function template)
  • iter_swap β€” swaps the values referenced by two dereferenceable objects (C++20) (customization point object)
  • iter_swap β€” swaps the objects pointed to by two underlying iterators (C++20) (function template)

🏷️ CATEGORY 🏷️

  • conditionally noexcept

Source/version: http://cppreference.com Β· date: 2024.06.10 β€” go swap something cute and be the main character of your own iterator story, bestie πŸ’–πŸ«Άβœ¨

πŸ“¦ where this page comes from