-1

I have wrote a utility named nostd::clip that clips a provided value x between a floor and a ceiling:

namespace nostd {
    template<class T>
    auto clip(T floor, T x, T ceiling) -> T
    {
        return std::min(ceiling, std::max(floor, x));
    }
}

Is there some function in std that does the same thing, that I could replace this with? Maybe in C++17?

Barry
  • 286,269
  • 29
  • 621
  • 977
nansy
  • 35
  • 2
  • 8
    You could just go through the list of utilities in the library? Wouldn't take more than a few minutes? And then you'd find out what else is there, too. Nobody studies any more :( – Lightness Races in Orbit Apr 26 '19 at 15:54

2 Answers2

5

You want std::clamp. It does what your custom implementation does.

You really should familiarise yourself with what the standard library provides. A reference site like cppreference is great for that.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
5

Are you looking for std::clamp? More info here.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78