Skip to content

Commit b6310e6

Browse files
committed
[libc++] Add lifetimebound attributes to clamp
Reviewed By: #libc, ldionne Spies: ldionne, arichardson, libcxx-commits Differential Revision: https://reviews.llvm.org/D158327
1 parent 1c35c1a commit b6310e6

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

libcxx/include/__algorithm/clamp.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,22 @@
2020
_LIBCPP_BEGIN_NAMESPACE_STD
2121

2222
#if _LIBCPP_STD_VER >= 17
23-
template<class _Tp, class _Compare>
24-
_LIBCPP_NODISCARD_EXT inline
25-
_LIBCPP_INLINE_VISIBILITY constexpr
26-
const _Tp&
27-
clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
28-
{
29-
_LIBCPP_ASSERT_UNCATEGORIZED(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
30-
return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
31-
23+
template <class _Tp, class _Compare>
24+
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
25+
clamp(_LIBCPP_LIFETIMEBOUND const _Tp& __v,
26+
_LIBCPP_LIFETIMEBOUND const _Tp& __lo,
27+
_LIBCPP_LIFETIMEBOUND const _Tp& __hi,
28+
_Compare __comp) {
29+
_LIBCPP_ASSERT_UNCATEGORIZED(!__comp(__hi, __lo), "Bad bounds passed to std::clamp");
30+
return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v;
3231
}
3332

34-
template<class _Tp>
35-
_LIBCPP_NODISCARD_EXT inline
36-
_LIBCPP_INLINE_VISIBILITY constexpr
37-
const _Tp&
38-
clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi)
39-
{
40-
return _VSTD::clamp(__v, __lo, __hi, __less<>());
33+
template <class _Tp>
34+
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
35+
clamp(_LIBCPP_LIFETIMEBOUND const _Tp& __v,
36+
_LIBCPP_LIFETIMEBOUND const _Tp& __lo,
37+
_LIBCPP_LIFETIMEBOUND const _Tp& __hi) {
38+
return _VSTD::clamp(__v, __lo, __hi, __less<>());
4139
}
4240
#endif
4341

libcxx/test/libcxx/algorithms/lifetimebound.verify.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ void func() {
4444
auto v7 = std::minmax(0, i, Comp{}); // expected-warning {{temporary whose address is used as value of local variable 'v7' will be destroyed at the end of the full-expression}}
4545
auto v8 = std::minmax(i, 0, Comp{}); // expected-warning {{temporary whose address is used as value of local variable 'v8' will be destroyed at the end of the full-expression}}
4646
}
47+
#if TEST_STD_VER >= 17
48+
{
49+
auto&& v1 = std::clamp(1, i, i); // expected-warning {{temporary bound to local reference 'v1' will be destroyed at the end of the full-expression}}
50+
auto&& v2 = std::clamp(i, 1, i); // expected-warning {{temporary bound to local reference 'v2' will be destroyed at the end of the full-expression}}
51+
auto&& v3 = std::clamp(i, i, 1); // expected-warning {{temporary bound to local reference 'v3' will be destroyed at the end of the full-expression}}
52+
auto&& v4 = std::clamp(1, i, i, Comp{}); // expected-warning {{temporary bound to local reference 'v4' will be destroyed at the end of the full-expression}}
53+
auto&& v5 = std::clamp(i, 1, i, Comp{}); // expected-warning {{temporary bound to local reference 'v5' will be destroyed at the end of the full-expression}}
54+
auto&& v6 = std::clamp(i, i, 1, Comp{}); // expected-warning {{temporary bound to local reference 'v6' will be destroyed at the end of the full-expression}}
55+
}
56+
#endif
4757
#if TEST_STD_VER >= 20
4858
{
4959
auto&& v1 = std::ranges::min(0, i); // expected-warning {{temporary bound to local reference 'v1' will be destroyed at the end of the full-expression}}

libcxx/utils/data/ignore_format.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
libcxx/include/__algorithm/binary_search.h
2-
libcxx/include/__algorithm/clamp.h
32
libcxx/include/__algorithm/comp_ref_type.h
43
libcxx/include/__algorithm/copy_backward.h
54
libcxx/include/__algorithm/copy_if.h

0 commit comments

Comments
 (0)