Most efficient/elegant way to clip a number?

What about boring, old, readable, and shortest yet:

float clip(float n, float lower, float upper) {
  return std::max(lower, std::min(n, upper));
}

?

This expression could also be 'genericized' like so:

template <typename T>
T clip(const T& n, const T& lower, const T& upper) {
  return std::max(lower, std::min(n, upper));
}

Update

Billy ONeal added:

Note that on windows you might have to define NOMINMAX because they define min and max macros which conflict


Why rewrite something that's already been written for you?

#include <boost/algorithm/clamp.hpp>
boost::algorithm::clamp(n, lower, upper);

As of C++17, this is now part of the STL:

#include <algorithm>
std::clamp(n, lower, upper);