diff options
author | Marshall Clow <mclow.lists@gmail.com> | 2016-03-07 22:43:49 +0000 |
---|---|---|
committer | Marshall Clow <mclow.lists@gmail.com> | 2016-03-07 22:43:49 +0000 |
commit | 146c14ac33136ebff2714f4596f6d7db85d6b5e8 (patch) | |
tree | 20a0dcbeb4d5cf436b8e467cdcd4ccb602077b90 /libcxx/include/algorithm | |
parent | b813e4d4ae0ab3bb3e7dbed0746180236ba52fbd (diff) | |
download | bcm5719-llvm-146c14ac33136ebff2714f4596f6d7db85d6b5e8.tar.gz bcm5719-llvm-146c14ac33136ebff2714f4596f6d7db85d6b5e8.zip |
Implement P0025R0: 'An algorithm to clamp a value between a pair of boundary values' for C++17
llvm-svn: 262871
Diffstat (limited to 'libcxx/include/algorithm')
-rw-r--r-- | libcxx/include/algorithm | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index 71e5df37dfe..7dba11e3002 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -543,6 +543,12 @@ template<class T, class Compare> T min(initializer_list<T> t, Compare comp); // constexpr in C++14 +template<class T> + constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17 + +template<class T, class Compare> + constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17 + template <class ForwardIterator> ForwardIterator max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14 @@ -2657,6 +2663,27 @@ max(initializer_list<_Tp> __t) #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS +#if _LIBCPP_STD_VER > 14 +// clamp +template<class _Tp, class _Compare> +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR +const _Tp& +clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp) +{ + _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp"); + return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v; + +} + +template<class _Tp> +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR +const _Tp& +clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi) +{ + return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>()); +} +#endif + // minmax_element template <class _ForwardIterator, class _Compare> |