25 Algorithms library [algorithms]

25.8 Sorting and related operations [alg.sorting]

25.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<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 = {});
Let comp be less{} for the overloads with no parameter comp, and let proj be identity{} for the overloads with no parameter proj.
Preconditions: bool(invoke(comp, invoke(proj, hi), invoke(proj, lo))) is false.
For the first form, type T meets the Cpp17LessThanComparable requirements (Table 28).
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.
[Note 1:
If NaN is avoided, T can be a floating-point type.
— end note]
Complexity: At most two comparisons and three applications of the projection.