[alg.clamp] (original) (raw)

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%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect 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 = {});

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.

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.