std::ranges...views::all_t(3)
π std::ranges::views::all, std::ranges::views::all_t π
okay bestie, this is a page straight out of the C++ Standard Library man pages (std::ranges...views::all_t(3), if we're being official about it) and honestly? it's a good one. let's get into it β¨
πΈ NAME πΈ
std::ranges::views::all, std::ranges::views::all_t β yes literally both of them, that's the whole name, they're a package deal π
π¦ SYNOPSIS π¦
bestie this all lives in the header <ranges>, no cap:
inline constexpr /* unspecified */ all = /* unspecified */; (1)
(since C++20)
template< ranges::viewable_range R > (2)
using all_t = decltype(views::all(std::declval<R>()));
(since C++20)
so here's the tea, number by number:
- this is a RangeAdaptorObject (and also a RangeAdaptorClosureObject, she's multi-talented π ) that hands you back a view containing, like, all the elements of whatever range you passed in. all of them. every last one. iconic.
say you've got an expression e of type R. then views::all(e) is expression-equivalent to:
- implicitly converting
eto astd::decay_t<R>prvalue β but ONLY ifstd::decay_t<R>modelsviewπ - otherwise,
std::ranges::ref_view{e}β but only if that expression is well-formed β¨ - otherwise (last resort era),
std::ranges::owning_view{e}π
- this one just calculates the suitable view type for a
viewable_rangetype. that's it. that's the whole job. main character energy but make it a type trait π«Ά
π₯ EXAMPLE π₯
run this code, i promise it's cute:
#include <iostream>
#include <ranges>
#include <type_traits>
#include <vector>
int main()
{
std::vector<int> v{0, 1, 2, 3, 4, 5};
for (int n : std::views::all(v) | std::views::take(2))
std::cout << n << ' ';
std::cout << '\n';
static_assert(std::is_same<
decltype(std::views::single(42)),
std::ranges::single_view<int>
>{});
static_assert(std::is_same<
decltype(std::views::all(v)),
std::ranges::ref_view<std::vector<int, std::allocator<int>>>
>{});
int a[]{1, 2, 3, 4};
static_assert(std::is_same<
decltype(std::views::all(a)),
std::ranges::ref_view<int[4]>
>{});
static_assert(std::is_same<
decltype(std::ranges::subrange{std::begin(a) + 1, std::end(a) - 1}),
std::ranges::subrange<int*, int*, std::ranges::subrange_kind(1)>
>{});
}
β OUTPUT β
0 1
(those static_asserts all pass too, obviously. slay.)
π DEFECT REPORTS π
so some behavior-changing defect reports got applied retroactively to previously published C++ standards β yes, time travel, they really did that π§
- LWG 3724, applied to C++20: the behavior as published said
views::allmight result in hard error for some move-only views β now the correct behavior is that it gets well-constrained π - P2415R2, applied to C++20: the behavior as published said
views::allreturned asubrangefor a non-view rvalue range β now the correct behavior is that it returns anowning_viewfor it π₯
π SEE ALSO π
ranges::empty_view/views::emptyβ an empty view with no elements.(class template)(variable template)(C++20)πranges::single_view/views::singleβ a view that contains a single element of a specified value.(class template)(customization point object)(C++20)πranges::owning_viewβ a view with unique ownership of some range.(C++20)(class template)πΈ
and that's the whole page, bestie, you ate it up fr fr π«Ά this little gem came from http://cppreference.com, dated 2024.06.10, and it goes by std::ranges...views::all_t(3) β go forth and view all the ranges, no cap πβ¨
π¦ 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