Handy Range Class

In my tradition of putting useful snippets of code up, this is a range class that I have found useful. Given two comparable values, it can do things within the min and max range. I’m sure it can be improved, but this has been sufficient for my uses.

template<typename T>
class Range {
    Range() {}
    Range( T min, T max ) : minval(min),maxval(max) { }

    const T getMin() const { return minval; }
    const T getMax() const { return maxval; }

    const T setMin(const T newval) { 
        T oldmin = minval; 
        minval = newval; 
        return oldmin; 
    const T setMax(const T newval) { 
        T oldmax = maxval; 
        maxval = newval; 
        return oldmax; 

    bool inRange(T checkval) const {
        return (checkval >= minval && 
                checkval <= maxval);

    bool isSubrange(T start, T end) const {
        return (minval>=start && minval<=end) || 
               (maxval>=start && maxval<=end);

        T minval;
        T maxval;

Related Posts:

This entry was posted in Programming and tagged , , , , , . Bookmark the permalink.

Comments are closed.