Boost logo

Boost :

Subject: Re: [boost] [type_traits] adding is_less_comparable<T>, etc.
From: Frédéric Bron (frederic.bron_at_[hidden])
Date: 2009-09-04 11:52:14


Please find at https://svn.boost.org/trac/boost/browser/sandbox/type_traits
and at http://dl.free.fr/tK0BwIzYy my new proposal according to review
comments.

* now using 2 template parameters so that it is possible to check if 2
different types are comparable
* now using factory functions instead of static references
* now using type_traits yes_type and no_type
* the doc. is alphabetically sorted
* the doc. has been updated for clarity
* the tests have been modified to include new possibilities

The code has the following properties:
* returns true if t<u is meaningful and returns a value convertible to bool
* returns false if t<u is meaningless.
* fails with compile time error if t<u is meaningful and returns void;
on this point, please have a look at the comment below.

Thank you for your review of this new version (please find below the
main piece of code).

Frédéric

> I'd like to see something done to detect the void return, with a meaningful error message though.
> It is possible to extract the return type from a function pointer and check for that being the same
> as void, so it might be possible to form a pointer to the operator. You'd have to know whether the
> argument types are built-in types, in which case you can short-circuit the answer. If not, you'd have
> to determine whether the operator is a free function or a member in order to create the proper
> pointer type. Once you have that, you can extract the return type and compare it. Finally, you can
> use BOOST_STATIC_ASSERT to complain if the return type is void.

This looks too difficult for me! Can we keep it for a future update,
unless you can show me how to do it?

type_traits/detail/is_comparable.hpp

#include <boost/config.hpp>
#include <boost/type_traits/remove_cv.hpp>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/detail/yes_no_type.hpp>

// should be the last #include
#include <boost/type_traits/detail/bool_trait_def.hpp>

namespace boost {
namespace detail {

// This namespace ensures that ADL does not mess things up.
namespace BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl) {

// a type returned from comparison operator when no such operator is
found in the
// type's own namespace
struct tag { } ;

// any soaks up implicit conversions and makes the following
// comparison operators less-preferred than any other such operators that
// might be found via ADL.
struct any { template <class T> any(T const&) ; } ;

// when operator< is not available, this one is used
tag operator BOOST_TT_TRAIT_OP (const any&, const any&) ;

::boost::type_traits::yes_type is_bool(bool) ; // this version is
preferred for types convertible to bool
::boost::type_traits::no_type is_bool(...) ; // this version is used otherwise

template < typename T, typename U >
struct BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl) {
       static T make_T() ;
       static U make_U() ;
       static const bool value =
               sizeof(is_bool(make_T() BOOST_TT_TRAIT_OP make_U())) ==
sizeof(::boost::type_traits::yes_type) ;
} ;

} // namespace impl
} // namespace detail

BOOST_TT_AUX_BOOL_TRAIT_DEF2(BOOST_TT_TRAIT_NAME,T,U=T,(::boost::detail::BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl)::BOOST_JOIN(BOOST_TT_TRAIT_NAME,_impl)<T,U>::value))

} // namespace boost

#include <boost/type_traits/detail/bool_trait_undef.hpp>


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