which is larger?

Started by
21 comments, last by Sneftel 14 years, 7 months ago
That looks right. Though the sadist in me wants to write:
template <typename typea, typename typeb>bool Is_A_GT_B(typea a, typeb b){	bool isASigned = std::numeric_limits<typea>::is_signed;	bool isBSigned = std::numeric_limits<typeb>::is_signed;	if(isASigned != isBSigned && (isASigned ? a : b) < 0) return isBSigned;	return a > b;}
Advertisement
Thanks for the discussion.

The signedness checks can be done statically to improve upon efficiency:

template <class A, class B, bool ASigned, bool BSigned>struct greater_impl{    static inline bool call(A a, B b) { return a > b; }};template <class A, class B>struct greater_impl<A, B, true, false>{    static inline bool call(A a, B b) {	if( a < 0 ) return false;        return a > b;    }};template <class A, class B>struct greater_impl<A, B, false, true>{    static inline bool call(A a, B b)    {	if( b < 0 ) return true;        return a > b;    }};template<class A, class B>inline bool safe_greater(A a, B b){	return greater_impl<A,B, std::numeric_limits<A>::is_signed, std::numeric_limits<B>::is_signed >::call(a,b);}
Because the is_signed field is a static const, there's no need to use template specialization here -- any sane compiler will remove the static branch automatically.

This topic is closed to new replies.

Advertisement