Boost logo

Boost :

Subject: Re: [boost] [type_traits] adding is_less_comparable<T>, etc.
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2009-09-01 16:08:53


Hi,
----- Original Message -----
From: "Frédéric Bron" <frederic.bron_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Tuesday, September 01, 2009 9:38 PM
Subject: Re: [boost] [type_traits] adding is_less_comparable<T>, etc.

 
> // two check overloads help us identify which comparison operator was picked
>
> // check(not convertible to bool) returns a reference to char[2] (sizeof>1)
> template <typename T> char (& check(T))[2] ;
>
> // check(convertible to bool) returns char (sizeof==1)
> template <typename T> char check(T*) ;
> char check(bool) ;
> char check(char) ;
> char check(signed char) ;
> char check(unsigned char) ;
> char check(short) ;
> char check(unsigned short) ;
> char check(int) ;
> char check(unsigned int) ;
> char check(long) ;
> char check(unsigned long) ;
>
> template < typename T, typename U >
> struct is_less_comparable_impl {
> static typename boost::remove_cv<T>::type &t ;
> static typename boost::remove_cv<U>::type &u ;
> static const bool value = sizeof(check(t<u))==1 ;

As sugested by Steven and Robert, have you tried

typedef char no;
struct yes { no dummy[2]; };

yes is_bool(bool);
no is_bool(...);
template < typename T, typename U >
struct is_less_comparable_impl {
static typename boost::remove_cv<T>::type &t ;
static typename boost::remove_cv<U>::type &u ;
static const bool value = sizeof(is_bool(t<u))==sizeof(yes) ;
}

instead of

> // two check overloads help us identify which comparison operator was picked
>
> // check(not convertible to bool) returns a reference to char[2] (sizeof>1)
> template <typename T> char (& check(T))[2] ;
>
> // check(convertible to bool) returns char (sizeof==1)
> template <typename T> char check(T*) ;
> char check(bool) ;
> char check(char) ;
> char check(signed char) ;
> char check(unsigned char) ;
> char check(short) ;
> char check(unsigned short) ;
> char check(int) ;
> char check(unsigned int) ;
> char check(long) ;
> char check(unsigned long) ;
>
> template < typename T, typename U >
> struct is_less_comparable_impl {
> static typename boost::remove_cv<T>::type &t ;
> static typename boost::remove_cv<U>::type &u ;
> static const bool value = sizeof(check(t<u))==1 ;

Best,
Vicente


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk