Skip to content

Instantly share code, notes, and snippets.

@BillyONeal
Created April 3, 2017 22:21
Show Gist options
  • Save BillyONeal/2e2d1fc2448d0027bae721d39fa961bd to your computer and use it in GitHub Desktop.
Save BillyONeal/2e2d1fc2448d0027bae721d39fa961bd to your computer and use it in GitHub Desktop.
MSVC++ std::clamp
#if _HAS_CXX17
// FUNCTION TEMPLATE clamp
template<class _Ty,
class _Pr>
constexpr const _Ty& clamp(const _Ty& _Val, const _Ty& _Min_val,
const _Ty& _Max_val, _Pr _Pred)
{ // returns _Val constrained to [_Min_val, _Max_val] ordered by _Pred
#if _ITERATOR_DEBUG_LEVEL == 2
return (_DEBUG_LT_PRED(_Pred, _Max_val, _Min_val)
? (_DEBUG_ERROR("invalid bounds arguments passed to std::clamp"), _Val)
: _DEBUG_LT_PRED(_Pred, _Max_val, _Val)
? _Max_val
: _DEBUG_LT_PRED(_Pred, _Val, _Min_val)
? _Min_val
: _Val);
#else /* ^^^ _ITERATOR_DEBUG_LEVEL == 2 ^^^ // vvv _ITERATOR_DEBUG_LEVEL != 2 vvv */
return (_Pred(_Max_val, _Val)
? _Max_val
: _Pred(_Val, _Min_val)
? _Min_val
: _Val);
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */
}
template<class _Ty>
constexpr const _Ty& clamp(const _Ty& _Val, const _Ty& _Min_val,
const _Ty& _Max_val)
{ // returns _Val constrained to [_Min_val, _Max_val]
return (_STD clamp(_Val, _Min_val, _Max_val, less<>()));
}
#endif /* _HAS_CXX17 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment