55 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03___ITERATOR_PREV_H11#define _LIBCPP___CXX03___ITERATOR_PREV_H12 13#include <__cxx03/__assert>14#include <__cxx03/__config>15#include <__cxx03/__iterator/advance.h>16#include <__cxx03/__iterator/iterator_traits.h>17#include <__cxx03/__type_traits/enable_if.h>18#include <__cxx03/__utility/move.h>19 20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21# pragma GCC system_header22#endif23 24_LIBCPP_PUSH_MACROS25#include <__cxx03/__undef_macros>26 27_LIBCPP_BEGIN_NAMESPACE_STD28 29template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>30inline _LIBCPP_HIDE_FROM_ABI _InputIter31prev(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n) {32 // Calling `advance` with a negative value on a non-bidirectional iterator is a no-op in the current implementation.33 // Note that this check duplicates the similar check in `std::advance`.34 _LIBCPP_ASSERT_PEDANTIC(__n <= 0 || __has_bidirectional_iterator_category<_InputIter>::value,35 "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");36 std::advance(__x, -__n);37 return __x;38}39 40// LWG 319741// It is unclear what the implications of "BidirectionalIterator" in the standard are.42// However, calling std::prev(non-bidi-iterator) is obviously an error and we should catch it at compile time.43template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>44[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _InputIter prev(_InputIter __it) {45 static_assert(__has_bidirectional_iterator_category<_InputIter>::value,46 "Attempt to prev(it) with a non-bidirectional iterator");47 return std::prev(std::move(__it), 1);48}49 50_LIBCPP_POP_MACROS51 52_LIBCPP_END_NAMESPACE_STD53 54#endif // _LIBCPP___CXX03___ITERATOR_PREV_H55