Search code examples
c++macrosclamp

Problem with my clamp macro


I have a problem with my clamp macro, when when my value is over 10 and my high is over 17 it stops working. Any idea?

#define CLAMP(value, low, high) (((value)<(low))?(low):(((value)>(high))?(high):(value)))

Solution

  • I would suggest using a safer way than a macro:

    template <typename T> T CLAMP(const T& value, const T& low, const T& high) 
    {
      return value < low ? low : (value > high ? high : value); 
    }