命名空间
变体
操作

std::ranges::crbegin

来自 cppreference.cn
< cpp‎ | ranges
 
 
范围库
范围适配器
 
定义于头文件 <ranges>
定义于头文件 <iterator>
inline namespace /* unspecified */ {

    inline constexpr /* unspecified */ crbegin = /* unspecified */;

}
(since C++20) (C++20 起)
(customization point object) (定制点对象)
调用签名
template< class T >

    requires /* see below */ (要求 /* 见下文 */)

constexpr /* see below */ auto crbegin( T&& t );
(since C++20) (C++20 起)

返回一个迭代器,指向限定为 const 的参数的首个元素,该参数被视为反向序列。

(until C++23) (C++23 前)

返回一个常量迭代器,指向被视为反向序列的参数的首个元素。

(since C++23) (C++23 起)

range-rbegin-rend.svg

CT

调用 ranges::crbegin 表达式等价于 ranges::rbegin(static_cast<CT&&>(t))

(until C++23) (C++23 前)

如果实参是左值,或 ranges::enable_borrowed_range<std::remove_cv_t<T>>true,则调用 ranges::crbegin 表达式等价于

在所有其他情况下,调用 ranges::crbegin 是非良构的,当调用出现在模板实例化的直接语境中时,可能导致替换失败

(since C++23) (C++23 起)

在所有情况下,返回类型均建模 std::input_or_output_iterator constant-iterator(C++23 起)

定制点对象

名称 ranges::crbegin 表示一个定制点对象,它是一个 const 函数对象,属于 字面 semiregular 类类型。 为了阐述目的,其类型的非 cv 限定版本表示为 __crbegin_fn

__crbegin_fn 的所有实例均相等。 在相同实参上调用类型 __crbegin_fn 的不同实例的效果是等效的,无论表示实例的表达式是左值还是右值,以及是否是 const 限定的(但是,不要求可调用 volatile 限定的实例)。 因此,ranges::crbegin 可以自由复制,并且其副本可以互换使用。

给定一组类型 Args...,如果 std::declval<Args>()... 满足上述 ranges::crbegin 实参的要求,则 __crbegin_fn 建模

否则,__crbegin_fn 的任何函数调用运算符均不参与重载决议。

[edit] 示例

#include <cassert>
#include <iterator>
#include <span>
#include <vector>
 
int main()
{
    std::vector<int> v{3, 1, 4};
    auto vi = std::ranges::crbegin(v);
    assert(*vi == 4);
    ++vi; // OK, iterator object is mutable
    assert(*vi == 1);
    // *vi = 13; // Error: underlying element is read-only
 
    int a[]{-5, 10, 15};
    auto ai = std::ranges::crbegin(a);
    assert(*ai == 15);
 
    // auto x_x = std::ranges::crbegin(std::vector<int>{6, 6, 6});
    // ill-formed: the argument is an rvalue (see Notes ↑)
 
    auto si = std::ranges::crbegin(std::span{a}); // OK
    assert(*si == 15);
    static_assert
    (
        std::ranges::enable_borrowed_range<std::remove_cv_t<decltype(std::span{a})>>
    );
}

[edit] 参见

返回范围的反向迭代器
(customization point object) (定制点对象)[编辑]
返回容器或数组开头的反向迭代器
(function template) (函数模板) [编辑]