std::ranges...value_result(3)
π std::ranges...value_result(3) π
okay bestie, this is the std::ranges...value_result(3) page from the C++ Standard Libary manual, and we are literally about to vibe-check a tiny C++23 class template. no cap, it's giving "iterator plus value, packaged cute" energy. π
π NAME π
std::ranges::out_value_result β std::ranges::out_value_result
β¨ Synopsis β¨
- defined in header
<algorithm> template< class O, class T >(since C++23)struct out_value_result;
ranges::out_value_result is a class template that provides a way to store an iterator and a value as a single unit.
This class template has no base classes or declared members other than those shown below. Thus it is suitable for use with structured bindings.
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.
πΈ Template parameters πΈ
O,Tβ the types of the objects that theranges::out_value_resultstores.
π Data members π
outβ a value (that is supposed to be an iterator) of typeO. (public member object)valueβ a value (that is supposed to be a stored value) of typeT. (public member object)
All these members are declared with [[no_unique_address]] attribute.
π Member functions π
std::ranges::out_value_result::operator out_value_result<O2, T2>
(1) β requires convertible_to<const O&, O2> && convertible_to<const T&, T2>:
template<class O2, class T2>
requires convertible_to<const O&, O2> && convertible_to<const T&, T2>
constexpr operator out_value_result<O2, T2>() const &;
(2) β requires convertible_to<O, O2> && convertible_to<T, T2>:
template<class O2, class T2>
requires convertible_to<O, O2> && convertible_to<T, T2>
constexpr operator out_value_result<O2, T2>() &&;
Converts *this to the result by constructing every data member of the result from the corresponding member of *this.
1)Equivalent toreturn {out, value};.2)Equivalent toreturn {std::move(out), std::move(value)};.
π Standard library π
The following standard library functions use ranges::out_value_result as the return type:
π₯ Algorithm functions π₯
ranges::iotafills a range with successive increments of the starting value (C++23) (niebloid)
π¦ Synopsis π¦
here's the full code reveal, bestie:
namespace std::ranges
{
template<class O, class T>
struct out_value_result
{
[[no_unique_address]] O out;
[[no_unique_address]] T value;
template<class O2, class T2>
requires convertible_to<const O&, O2> && convertible_to<const T&, T2>
constexpr operator out_value_result<O2, T2>() const &
{
return {out, value};
}
template<class O2, class T2>
requires convertible_to<O, O2> && convertible_to<T, T2>
constexpr operator out_value_result<O2, T2>() &&
{
return {std::move(out), std::move(value)};
}
};
}
π₯ 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.
Unlike std::pair and std::tuple, this class template has data members of meaningful names.
π Example π
// Run this code
#include <algorithm>
#include <array>
#include <cassert>
#include <numeric>
#include <ranges>
int main()
{
std::array<int, 4> a{};
constexpr std::array expected{2, 3, 4, 5};
const auto result = std::ranges::iota(a, 2);
assert(std::ranges::distance(a.cbegin(), result.out) == 4);
assert(result.value == 6);
assert(a == expected);
}
π 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 structured bindings π β this page came from http://cppreference.com, dated 2024.06.10, and the manual page is std::ranges...value_result(3). periodt. π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms