Namespaces
Variants
Views
Actions

std::ranges::viewable_range

From cppreference.com
< cpp‎ | ranges
 
 
Ranges library
Range access
Range conversions
(C++23)

Range primitives



Dangling iterator handling
Range concepts
viewable_range

Views

Range factories
Range adaptors
Range generators
Range adaptor objects
Range adaptor closure objects
Helper items
(until C++23)(C++23)


 
Defined in header <ranges>
template< class T >

concept viewable_range =
    ranges::range<T> &&
    ((ranges::view<std::remove_cvref_t<T>> &&
      std::constructible_from<std::remove_cvref_t<T>, T>) ||
     (!ranges::view<std::remove_cvref_t<T>> &&
      (std::is_lvalue_reference_v<T> ||

       (std::movable<std::remove_reference_t<T>> && !/*is-initializer-list*/<T>))));
(since C++20)

The viewable_range concept is a refinement of range that describes a range that can be converted into a view through views::all.

The constant /*is-initializer-list*/<T> is true if and only if std::remove_cvref_t<T> is a specialization of std::initializer_list.

[edit] Example

#include <ranges>
#include <string>
#include <vector>
 
struct valid_result {};
struct invalid_result {};
 
template <typename T>
concept valid_viewable_range = std::same_as<T, valid_result>;
 
template <typename T>
concept invalid_viewable_range = std::same_as<T, invalid_result>;
 
auto test_viewable_range(std::ranges::viewable_range auto &&) -> valid_result;
auto test_viewable_range(auto&&) -> invalid_result;
 
int main()
{
    auto il = {1, 2, 3};
    int arr []{1, 2, 3};
    std::vector vec{1, 2, 3};
    std::ranges::ref_view r{arr};
    std::ranges::owning_view o{std::string("Hello")};
 
    static_assert(requires {
        { test_viewable_range(il) } -> valid_viewable_range;
        { test_viewable_range(std::move(il)) } -> invalid_viewable_range;
        { test_viewable_range(arr) } -> valid_viewable_range;
        { test_viewable_range(std::move(arr)) } -> invalid_viewable_range;
        { test_viewable_range(vec) } -> valid_viewable_range;
        { test_viewable_range(std::move(vec)) } -> valid_viewable_range;
        { test_viewable_range(r) } -> valid_viewable_range;
        { test_viewable_range(std::move(r)) } -> valid_viewable_range;
        { test_viewable_range(o) } -> invalid_viewable_range;
        { test_viewable_range(std::move(o)) } -> valid_viewable_range;
        { test_viewable_range(std::ranges::ref_view(o)) } -> valid_viewable_range;
    });
}

[edit] Defect reports

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

DR Applied to Behavior as published Correct behavior
LWG 3481 C++20 viewable_range accepted an lvalue of a move-only view rejects
P2415R2 C++20 viewable_range only accepted non-view rvalues that are borrowed_range accepts more types

[edit] See also

a view that includes all elements of a range
(alias template) (range adaptor object)[edit]