Author: ericwf Date: Wed Aug 22 10:47:13 2018 New Revision: 340426 URL: http://llvm.org/viewvc/llvm-project?rev=340426&view=rev Log: Add diagnostics for min/max algorithms when a InputIterator is used.
These algorithms require a ForwardIterator or better. Ensure we diagnose the contract violation at compile time instead of of silently doing the wrong thing. Further algorithms will be audited in upcoming patches. Added: libcxx/trunk/test/std/algorithms/alg.sorting/alg.min.max/requires_forward_iterator.fail.cpp Modified: libcxx/trunk/include/algorithm Modified: libcxx/trunk/include/algorithm URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/algorithm?rev=340426&r1=340425&r2=340426&view=diff ============================================================================== --- libcxx/trunk/include/algorithm (original) +++ libcxx/trunk/include/algorithm Wed Aug 22 10:47:13 2018 @@ -2398,6 +2398,8 @@ inline _LIBCPP_INLINE_VISIBILITY _LIBCPP _ForwardIterator min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) { + static_assert(__is_forward_iterator<_ForwardIterator>::value, + "std::min_element requires a ForwardIterator"); if (__first != __last) { _ForwardIterator __i = __first; @@ -2462,6 +2464,8 @@ inline _LIBCPP_INLINE_VISIBILITY _LIBCPP _ForwardIterator max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) { + static_assert(__is_forward_iterator<_ForwardIterator>::value, + "std::max_element requires a ForwardIterator"); if (__first != __last) { _ForwardIterator __i = __first; @@ -2548,6 +2552,8 @@ _LIBCPP_CONSTEXPR_AFTER_CXX11 std::pair<_ForwardIterator, _ForwardIterator> minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) { + static_assert(__is_forward_iterator<_ForwardIterator>::value, + "std::minmax_element requires a ForwardIterator"); std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first); if (__first != __last) { Added: libcxx/trunk/test/std/algorithms/alg.sorting/alg.min.max/requires_forward_iterator.fail.cpp URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.sorting/alg.min.max/requires_forward_iterator.fail.cpp?rev=340426&view=auto ============================================================================== --- libcxx/trunk/test/std/algorithms/alg.sorting/alg.min.max/requires_forward_iterator.fail.cpp (added) +++ libcxx/trunk/test/std/algorithms/alg.sorting/alg.min.max/requires_forward_iterator.fail.cpp Wed Aug 22 10:47:13 2018 @@ -0,0 +1,37 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <algorithm> + +// template<ForwardIterator Iter> +// max_element(Iter first, Iter last); + +#include <algorithm> +#include <cassert> + +#include "test_iterators.h" + +int main() { + int arr[] = {1, 2, 3}; + const int *b = std::begin(arr), *e = std::end(arr); + typedef input_iterator<const int*> Iter; + { + // expected-error@algorithm:* {{"std::min_element requires a ForwardIterator"}} + std::min_element(Iter(b), Iter(e)); + } + { + // expected-error@algorithm:* {{"std::max_element requires a ForwardIterator"}} + std::max_element(Iter(b), Iter(e)); + } + { + // expected-error@algorithm:* {{"std::minmax_element requires a ForwardIterator"}} + std::minmax_element(Iter(b), Iter(e)); + } + +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits