Boost logo

Boost :

Subject: Re: [boost] review request: addition to type_traits library ofhas_operator_xxx
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2010-11-17 13:46:20


----- Original Message -----
From: "Edward Diener" <eldiener_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, November 17, 2010 1:29 AM
Subject: Re: [boost] review request: addition to type_traits library ofhas_operator_xxx

On 11/16/2010 6:49 PM, Frédéric Bron wrote:
> I would like to propose to your review the following addition to the
> type_traits library, available at the following addresses:
> https://svn.boost.org/trac/boost/browser/sandbox/type_traits
> http://dl.free.fr/lRm4VL6WP/type_traits.tar.bz2
>
> The purpose of the addition is to add type traits to detect if unary
> and binary operators can be applied to given types.
> For example, is "x<y" or "!x" or "x+y" meaningful?
> If required, the return type of such an expression is checked to know
> if it is convertible to a given type.
> Default behaviour is to not check the return type.
>
> The following traits are added:
>
> // binary operators:
> template< typename LHS, typename RHS=LHS, typename RET=void>
> == has_operator_equal_to
> != has_operator_not_equal_to
> > has_operator_greater
> >= has_operator_greater_equal
> < has_operator_less
> <= has_operator_less_equal
> + has_operator_plus
> - has_operator_minus
> * has_operator_multiplies
> / has_operator_divides
> % has_operator_modulus
> && has_operator_logical_and
> || has_operator_logical_or
> & has_operator_bit_and
> | has_operator_bit_or
> ^ has_operator_bit_xor
>
> // unary operators:
> template< typename RHS, typename RET=void>
> + has_operator_unary_plus
> - has_operator_unary_minus
> ! has_operator_logical_not
>
> This new version reflects the discussions we had on the list:
> http://thread.gmane.org/gmane.comp.lib.boost.devel/194625
> In particular about the check or not of the return type.
> All operators are now included and not only comparison binary operators.
>
> Example:
>
> has_operator_less<LHS, RHS, RET>::value_type is the type bool.
> has_operator_less<int> inherits from true_type.
> has_operator_less<int, int, std::string> inherits from false_type.
> has_operator_unary_minus<int, long> inherits from true_type.
> has_operator_unary_minus<double, int> inherits from true_type.
> has_operator_unary_minus<int, std::string> inherits from false_type.
>
> Documentation is accessible at libs/type_traits/doc/html/index.html in
> the archive.

Bravo !

I would very much like to see this happen also, as I can use this
functionality in another library I would like to put in the sandbox.

Without trying to make more work along your lines, would it be easy
enough for you to add the left shift ( << ) and right shift ( >> )
binary operators, and/or the incrementable ( ++x and x++ ) and
decrementable ( --x and x-- ) unary operators, or are any of these
especially different or much more difficult cases ?

_______________________________________________

Hi,

Thanks for pushing these long expected traits.

I would expect all the C++ operators to be covered as ConceptTraits/OperatorTrats did. Next follows an extract from the documentation of this library for the missing operators. BTW, where are the new extensions documented, could you give a link?

::boost::has_address_of_op<T>::value
Evaluates to true if the expression &a is valid.

::boost::has_dereference_op<T>::value

Evaluates to true if the expression *a is valid.

::boost::has_member_access_op<T>::value

Evaluates to true if the expression a-><member> is valid.

The member access operator has to be a member function, so it can only be detected for classes and unions by specialising this trait. It gives default true for these.

::boost::has_subscript_op<T1 [,T2]>::value

Evaluates to true if the expression a[b] is valid.

The subscript operator has to be a member function, so it can only be detected for classes and unions by specialising this trait. It gives default true for these.

::boost::has_pointer_to_member_op<T1 [,T2]>::value

Evaluates to true if the expression a->*b is valid.

The pointer to member operator has to be a member function, so it can only be detected for classes and unions by specialising this trait. It gives default true for these.

::boost::has_comma_op<T1 [,T2]>::value

Evaluates to true if the expression a,b is valid.

::boost::has_function_call_op<T,R [,P1, ...]>::value

Evaluates to true if the expression
R r = a(p1 [, ...]) is valid.

The function call operator has to be a member function, so it can only be detected for classes and unions by specialising this trait. It gives default true for these.

Best,

Vicente


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