std::ranges..._out_result(3)

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

πŸŽ€ NAME πŸŽ€

okay so this is the page for std::ranges..._out_result(3) from the C++ Standard Library β€” yes, the header has that C++ Standard Libary typo energy, but we're not gonna roast it, we're just here for the iterators. The NAME vibe is literally:

std::ranges::in_out_out_result β€” std::ranges::in_out_out_result


✨ Synopsis ✨

Defined in header <algorithm>:

template< class I, class O1, class O2 >  (since C++20)
struct in_out_out_result;

ranges::in_out_out_result is a class template that provides a way to store three iterators as a single unit. Three iterators, one little bundle β€” it's giving main-character convenience πŸ’….

This class template has no base classes or declared members other than those shown below. Thus it is suitable for use with structured bindings. Bestie, that means it's literally built for cute unpacking.

All special member functions of this class template are implicitly declared, which makes specializations be aggregate classes, and propagate triviality, potentially-throwing-ness, and constexpr-ness of corresponding operations on data members. Slay for not making us write them ourselves ✨.

🌸 Template parameters 🌸

  • I, O1, O2 β€” the types of the objects that ranges::in_out_out_result stores.

πŸ“ Data members πŸ“

  • in β€” a value (that is supposed to be an iterator) of type I. (public member object)
  • out1 β€” a value (that is supposed to be an iterator) of type O1. (public member object)
  • out2 β€” a value (that is supposed to be an iterator) of type O2. (public member object)

All these members are declared with [[no_unique_address]] attribute. No cap, that attribute is doing its little optimization moment 🫢.


🫢 Member functions 🫢

std::ranges::in_out_out_result::operator in_out_out_result<II, OO1, OO2>

template<class II, class OO1, class OO2>

requires std::convertible_to<const I&, II> &&
         std::convertible_to<const O1&, OO1> &&               (1)
         std::convertible_to<const O2&, OO2>

constexpr operator in_out_out_result<II, OO1, OO2>() const &;

template<class II, class OO1, class OO2>

requires std::convertible_to<I, II> &&
         std::convertible_to<O1, OO1> &&                      (2)
         std::convertible_to<O2, OO2>

constexpr operator in_out_out_result<II, OO1, OO2>() &&;

Converts *this to the result by constructing every data member of the result from the corresponding member of *this. Every member gets matched up like it's a group chat invite list πŸ’–.

  1. Equivalent to return {in, out1, out2};.
  2. Equivalent to return {std::move(in), std::move(out1), std::move(out2)};.

πŸ‘‘ Standard library πŸ‘‘

The following standard library functions use ranges::in_out_out_result as the return type:

🧚 Algorithm functions 🧚

  • ranges::partition_copy β€” copies a range dividing the elements into two groups (C++20) (niebloid).

🌈 Synopsis 🌈

namespace std::ranges
{
    template<class I, class O1, class O2>
    struct in_out_out_result
    {
        [[no_unique_address]] I  in;
        [[no_unique_address]] O1 out1;
        [[no_unique_address]] O2 out2;

        template<class II, class OO1, class OO2>
        requires std::convertible_to<const I&, II> &&
                 std::convertible_to<const O1&, OO1> &&
                 std::convertible_to<const O2&, OO2>
        constexpr operator in_out_out_result<II, OO1, OO2>() const &
        {
            return {in, out1, out2};
        }

        template<class II, class OO1, class OO2>
        requires std::convertible_to<I, II> &&
                 std::convertible_to<O1, OO1> &&
                 std::convertible_to<O2, OO2>
        constexpr operator in_out_out_result<II, OO1, OO2>() &&
        {
            return {std::move(in), std::move(out1), std::move(out2)};
        }
    };
}

πŸ’œ Notes πŸ’œ

Each standard library algorithm that uses this family of return types declares a new alias type, e.g. using merge_result = in_in_out_result<I1, I2, O>;.

The names for such aliases are formed by adding the suffix "_result" to the algorithm's name. So, the return type of std::ranges::merge can be named as std::ranges::merge_result. iykyk, it's a whole naming convention moment ✨.

Unlike std::pair and std::tuple, this class template has data members of meaningful names. Not me being obsessed with readable member names 😭.


πŸ¦‹ Example πŸ¦‹

// Run this code

#include <algorithm>
#include <array>
#include <cctype>
#include <iostream>
#include <iterator>
#include <ranges>
#include <string_view>

void print(std::string_view rem, auto first, auto last)
{
    for (std::cout << rem << ": { "; first != last; ++first)
        std::cout << *first << ' ';
    std::cout << "}\n";
}

int main()
{
    constexpr std::string_view in {"TvEeNcStOoRr"};
    std::array<char, in.size()> o1, o2;

    const auto result = std::ranges::partition_copy(in, o1.begin(), o2.begin(),
        [](char c) { return std::isupper(c); });

    print("in", in.begin(), result.in);
    print("o1", o1.begin(), result.out1);
    print("o2", o2.begin(), result.out2);
}

πŸ“ Output πŸ“

in: { T v E e N c S t O o R r }
o1: { T E N S O R }
o2: { v e c t o r }

⭐ See also ⭐

  • pair β€” implements binary tuple, i.e. a pair of values (class template)
  • tuple β€” implements fixed size container, which holds elements of possibly different (C++11) types (class template)

kisses and iterator dust πŸ’– source: http://cppreference.com β€’ version/date: 2024.06.10 β€’ page: std::ranges..._out_result(3) πŸŽ€

πŸ“¦ where this page comes from