Checking if an input is within its range of limits in C++ -
i need create multiple functions check if input valid or not. here of codes:
bool isvalidrange(signed char s) { bool isvalid = true; if (!((s>=schar_min)&&(s<=schar_max))) { isvalid = false; } return isvalid; } bool isvalidrange(int s) { bool isvalid = true; if (!((s>=int_min)&&(s<=int_max))) { isvalid = false; } return isvalid; } i'm using header limits.h this. doing right way? please take note i'm beginner. hope understand. thank you!
the template above not work mixture of signed , unsigned types, in addition floats.
template<typename rangetype, typename valuetype > bool isinrange( valuetype value ) { if( ! numeric_limits<rangetype>::is_integer ) { return (value > 0 ? value : -value) <= numeric_limits<rangetype>::max(); } if ( numeric_limits<rangetype>::is_signed == numeric_limits<valuetype>::is_signed ) { return value >= numeric_limits<rangetype>::min() && value <= numeric_limits<rangetype>::max(); } else if( numeric_limits<rangetype>::is_signed ) { return value <= numeric_limits<rangetype>::max(); } else { return value >= 0 && value <= numeric_limits<rangetype>::max(); } }
Comments
Post a Comment