I found myself having to write a bunch of range checking code like this:

so I wrote this simple function.

So the above code gets simplified to:

I wasn’t happy about the extra copy being created but was willing to live with it.
Then I ran across this fragment which is so similar but ended up changing the final design:

The difference is the above check sets an arbitrary value if it wasn’t within the range instead of just assigning one of the bounds to the value. That caused me to split it into a test, within(), to handle this case and a setwithin() to handle the earlier cases.
Splitting it also let me do a minor optimization. The typical case is that being out of bounds is rather exceptional and it’d be nice to avoid doing a copy. Accordingly, setwithin() checks if the dest has the same address as the replacement value and avoids doing the assignment.