The clamp functions are badly designed

Xavier Jouvenot
4 min readOct 5, 2022

Hello ! I’m Xavier Jouvenot and today, we are going to see why the std::clamp and std::ranges::clamp functions of the C++ standard are badly designed and how we can simply improve it.

Self promotion: Here are a few social networks where you can follow me and check my work as a programmer and a writer 😉 Personal blog, Twitter, GitHub

The issue with std::clamp and std::ranges::clamp

Since C++17, the function std::clamp is available as a helper to keep a variable in a range of value defined by 2 variables specified during the function call. In C++20, the function std::ranges::clamp was also added to the C++ standard.

Those function are defined as such:

namespace std
{
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 );
namespace ranges
{
template< class T, class Proj = std::identity, std::indirect_strict_weak_order<std::projected<const T*, Proj>> Comp = ranges::less >
constexpr const T&
clamp( const T& v, const T& lo, const T& hi, Comp comp = {}, Proj proj = {} );
} // namespace ranges
} // namespace std

And code using those function can look like that:

auto value = std::clamp (variable, 0, 100);
auto other_value = std::ranges::clamp (other_variable, -100, 0);

--

--