templates - c++ generic way to check a value in a range -
in maths there different kind of range: can open ( (a, b) ), close ( [a, b] ), left opened (a, b] ) or right opened ( [a, b) ).
want write template function in c++ (not 11) can easly manage these situation. i'm not confident meta-programming , templates.
want have this:
const int max = max, int min = min; int x = value; // check close range if ( is_in_range( x, min, max ) ) //... if ( is_in_range( x, min, max, open) ) //... if ( is_in_range( x, min, max, left_open) ) //... if ( is_in_range( x, min, max, right_open) ) //...
have suggest?
edit 1
i've tryed cannot compile
enum { range_open, range_close, range_left_open, range_right_open }; namespace detail { template < typename type > inline bool check_open_range( const type& x, const type& max, const type& min ) { return ( min < x ) && ( x < max ); } template < typename type > inline bool check_close_range( const type& x, const type& max, const type& min ) { return ( min <= x ) && ( x <= max ); } template < typename type > inline bool check_left_open_range( const type& x, const type& max, const type& min ) { return ( min < x ) && ( x <= max ); } template < typename type > inline bool check_right_open_range( const type& x, const type& max, const type& min ) { return ( min <= x ) && ( x < max ); } } template < typename type, int range_open > inline bool check_range( const type& x, const type& max, const type& min ); template < typename type, range_open > inline bool check_range( const type& x, const type& max, const type& min ) { return detail::check_open_range( x, min, max ); } template < typename type, range_close > inline bool check_range( const type& x, const type& max, const type& min ) { return detail::check_close_range( x, min, max ); } template < typename type, check_left_open_range > inline bool check_range( const type& x, const type& max, const type& min ) { return detail::check_left_open_range( x, min, max ); } template < typename type, check_right_open_range > inline bool check_range( const type& x, const type& max, const type& min ) { return detail::check_right_open_range( x, min, max ); }
effectly more simple 4 overloaded functions
edit 2
namespace detail { template < typename type > inline bool check_open_range( const type& x, const type& max, const type& min ) { return ( min < x ) && ( x < max ); } template < typename type > inline bool check_close_range( const type& x, const type& max, const type& min ) { return ( min <= x ) && ( x <= max ); } template < typename type > inline bool check_left_open_range( const type& x, const type& max, const type& min ) { return ( min < x ) && ( x <= max ); } template < typename type > inline bool check_right_open_range( const type& x, const type& max, const type& min ) { return ( min <= x ) && ( x < max ); } } struct range_open {}; struct range_close {}; struct left_open_range {}; struct right_open_range {}; template < typename type > inline bool check_range( const type& x, const type& max, const type& min) { return detail::check_open_range( x, min, max ); } template < typename type > inline bool check_range( const type& x, const type& max, const type& min, const range_open&) { return detail::check_open_range( x, min, max ); } template < typename type > inline bool check_range( const type& x, const type& max, const type& min, const range_close& ) { return detail::check_close_range( x, min, max ); } template < typename type > inline bool check_range( const type& x, const type& max, const type& min, const left_open_range& ) { return detail::check_left_open_range( x, min, max ); } template < typename type > inline bool check_range( const type& x, const type& max, const type& min, const right_open_range& ) { return detail::check_right_open_range( x, min, max ); }
think edit 2 right...
edit 3: version
struct left_open { template static bool compare (const t& value, const t& range) { return range < value; } };
struct left_close { template <class t> static bool compare (const t& value, const t& range) { return range <= value; } }; struct right_open { template <class t> static bool compare (const t& value, const t& range) { return value < range; } }; struct right_close { template <class t> static bool compare (const t& value, const t& range) { return value <= range; } }; template <class l, class r, class t> bool check_range(const t& value, const t& min, const t& max) { return l::compare <t> (value, min) && r::compare <t> (value, max); }
here works on vs2010:-
class leftopen { public: template <class t> static bool compare (const t value, const t range) { return value >= range; } }; class leftclosed { public: template <class t> static bool compare (const t value, const t range) { return value > range; } }; class rightopen { public: template <class t> static bool compare (const t value, const t range) { return value <= range; } }; class rightclosed { public: template <class t> static bool compare (const t value, const t range) { return value < range; } }; template <class l, class r, class t> bool isinrange (t value, t min, t max) { return l::compare <t> (value, min) && r::compare <t> (value, max); } int main() { int min = 5, max = 99; bool r1 = isinrange <leftopen, rightopen> (-19, min, max), r2 = isinrange <leftopen, rightopen> (45, min, max), r3 = isinrange <leftopen, rightopen> (149, min, max); }
Comments
Post a Comment