Boost logo

Boost :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2002-03-22 13:14:15


David Abrahams wrote:
> The biggest problem here is that you have to know not only
> the arity but the argument and return types for the member
> function. That tends to limit applicability of the technique.

Agree, but it's still better than nothing. For one, operator overloads tend
(and sometimes required) to have a particular signature, and the technique
allows you to do something like this:

typedef char (&no_tag)[1];
typedef char (&yes_tag)[2];

template < typename T, typename U, T (T::*)(U) > struct op_mf1 {};
template < typename T, typename U, T& (T::*)(U) > struct op_mf2 {};
template < typename T, typename U, T (T::*)(U const&) > struct op_mf3 {};
template < typename T, typename U, T& (T::*)(U const&) > struct op_mf4 {};

template< typename T, typename U > no_tag has_plus_assignment_helper(...);
template< typename T, typename U > yes_tag
has_plus_assignment_helper(op_mf1< T,U,&T::operator+= >*);
template< typename T, typename U > yes_tag
has_plus_assignment_helper(op_mf2< T,U,&T::operator+= >*);
template< typename T, typename U > yes_tag
has_plus_assignment_helper(op_mf3< T,U,&T::operator+= >*);
template< typename T, typename U > yes_tag
has_plus_assignment_helper(op_mf4< T,U,&T::operator+= >*);

template< typename T, typename U = T >
struct has_plus_assignment
{
    BOOST_STATIC_CONSTANT(bool
        , value = sizeof(has_plus_assignment_helper<T,U>(0)) ==
sizeof(yes_tag)
        );
};

struct my {};
struct her { her& operator+=(int); };
struct their { their& operator+=(their const&); };

BOOST_STATIC_ASSERT((!has_plus_assignment<my,int>::value));
BOOST_STATIC_ASSERT((has_plus_assignment<her,int>::value));
BOOST_STATIC_ASSERT((has_plus_assignment<their>::value));

Aleksey


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