Boost logo

Boost :

Subject: [boost] review request: addition to type_traits library of has_operator_xxx
From: Frédéric Bron (frederic.bron_at_[hidden])
Date: 2010-11-16 18:49:26

I would like to propose to your review the following addition to the
type_traits library, available at the following addresses:

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:
In particular about the check or not of the return type.
All operators are now included and not only comparison binary operators.


    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.



PS: tested with g++ 4.4.5 on i686-linux.

Boost list run by bdawes at, gregod at, cpdaniel at, john at