Files
cppdraft_translate/cppdraft/alg/clamp.md
2025-10-25 03:02:53 +03:00

2.3 KiB
Raw Blame History

[alg.clamp]

26 Algorithms library [algorithms]

26.8.10 Bounded value [alg.clamp]

🔗

template<class T> constexpr const T& clamp(const T& v, const T& lo, const T& hi); template<class T, class Compare> constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp); template<class T, class Proj = identity, [indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect_strict_weak_order "24.3.6.3Indirect callables[indirectcallable.indirectinvocable]")<projected<const T*, Proj>> Comp = ranges::less> constexpr const T& ranges::clamp(const T& v, const T& lo, const T& hi, Comp comp = {}, Proj proj = {});

1

#

Let comp be less{} for the overloads with no parameter comp, and let proj be identity{} for the overloads with no parameter proj.

2

#

Preconditions: bool(invoke(comp, invoke(proj, hi), invoke(proj, lo))) is false.

For the first form, type T meets the Cpp17LessThanComparable requirements (Table 29).

3

#

Returns: lo if bool(invoke(comp, invoke(proj, v), invoke(proj, lo))) is true,hi if bool(invoke(comp, invoke(proj, hi), invoke(proj, v))) is true, otherwise v.

4

#

[Note 1:

If NaN is avoided, T can be a floating-point type.

— end note]

5

#

Complexity: At most two comparisons and three applications of the projection.