math.h
goes to the trouble of providing min
and max
, but not a clamp function. I would have thought that as they are all usually similar implementation-wise they would all appear in the same library.
Is there any particular reason that math.h
does not feature a clamp function|macro?
Is it that the creators of math.h
did not deem it necessary or did they just not think about it?
EDIT: It seems people are missing the point here. I'm not asking why didn't they add clamp because I'm lazy and don't like writing a new clamp - quite the opposite, I hardly ever use it (though admittedly I use it more than I use some of the standard libraries).
What I'm asking is "does anyone know of any reason why the c++ standardisation authorities or creators or whoever chose not to include a clamp function in math.h
?".
I am by no means complaining that it is not in math.h
, I am merely asking "is there a good reason it isn't there?".
EDIT:
I am explicitly not asking how to write a clamp()
function.
The other answers are no longer valid, as std::clamp is now in C++17.
At the time of writing it isn't supported by GCC, but will be in GCC 7.