Yes there is:
if (var < 32767) var++;
By the way, you shouldn't hard code the constant, use the ones numeric_limits<short>::max()defined in the header instead <limits>.
You can encapsulate this functionality in a function template:
template <class T>
void increment_without_wraparound(T& value) {
if (value < numeric_limits<T>::max())
value++;
}
and use it like:
short var = 32767;
increment_without_wraparound(var);
source
share