rcpputils  master
C++ API providing common utilities and data structures.
Functions
rcppmath Namespace Reference

Functions

template<class T >
constexpr const T & clamp (const T &v, const T &lo, const T &hi)
 If v compares less than lo, returns lo; otherwise if hi compares less. More...
 
template<class T , class Compare >
constexpr const T & clamp (const T &v, const T &lo, const T &hi, Compare comp)
 Like the function above, but uses comp to compare the values. More...
 

Function Documentation

◆ clamp() [1/2]

template<class T >
constexpr const T& rcppmath::clamp ( const T &  v,
const T &  lo,
const T &  hi 
)
constexpr

If v compares less than lo, returns lo; otherwise if hi compares less.

Parameters
[in]vthe value to clamp
[in]lothe lower boundary
[in]hithe higher boundary
Returns
Reference to lo if v is less than lo, reference to hi if hi is less than v, otherwise reference to v.
Note
Implementation from https://en.cppreference.com/w/cpp/algorithm/clamp
Warning
Capturing the result of clamp by reference if one of the parameters is rvalue produces a dangling reference if that parameter is returned

◆ clamp() [2/2]

template<class T , class Compare >
constexpr const T& rcppmath::clamp ( const T &  v,
const T &  lo,
const T &  hi,
Compare  comp 
)
constexpr

Like the function above, but uses comp to compare the values.

Parameters
[in]compComparison object that returns true if the first argument is less than the second