Path: blob/main/contrib/llvm-project/libcxx/include/__iterator/prev.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___ITERATOR_PREV_H10#define _LIBCPP___ITERATOR_PREV_H1112#include <__assert>13#include <__config>14#include <__iterator/advance.h>15#include <__iterator/concepts.h>16#include <__iterator/incrementable_traits.h>17#include <__iterator/iterator_traits.h>18#include <__type_traits/enable_if.h>1920#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21# pragma GCC system_header22#endif2324_LIBCPP_BEGIN_NAMESPACE_STD2526template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>27inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter28prev(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) {29// Calling `advance` with a negative value on a non-bidirectional iterator is a no-op in the current implementation.30// Note that this check duplicates the similar check in `std::advance`.31_LIBCPP_ASSERT_PEDANTIC(__n <= 0 || __has_bidirectional_iterator_category<_InputIter>::value,32"Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");33std::advance(__x, -__n);34return __x;35}3637#if _LIBCPP_STD_VER >= 203839// [range.iter.op.prev]4041namespace ranges {42namespace __prev {4344struct __fn {45template <bidirectional_iterator _Ip>46_LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x) const {47--__x;48return __x;49}5051template <bidirectional_iterator _Ip>52_LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {53ranges::advance(__x, -__n);54return __x;55}5657template <bidirectional_iterator _Ip>58_LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Ip __bound_iter) const {59ranges::advance(__x, -__n, __bound_iter);60return __x;61}62};6364} // namespace __prev6566inline namespace __cpo {67inline constexpr auto prev = __prev::__fn{};68} // namespace __cpo69} // namespace ranges7071#endif // _LIBCPP_STD_VER >= 207273_LIBCPP_END_NAMESPACE_STD7475#endif // _LIBCPP___ITERATOR_PREV_H767778