Boost logo

Boost :

Subject: Re: [boost] [Review] TTI
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2011-07-27 16:14:07


On Wed, Jul 27, 2011 at 12:23 PM, Mathias Gaunard <
mathias.gaunard_at_[hidden]> wrote:

> On 07/27/2011 07:52 PM, Jeffrey Lee Hellrung, Jr. wrote:
>
> I think you missed the convertibility requirement(s).
>>>>
>>>>
>>> TTI needs an exact signature.
>>>
>>>
>> ...and hence why I had presumed it to be outside of the scope of TTI. It
>> stills falls within the domain of introspection, though.
>>
>
> You need SFINAE for expressions to do this.
>

You can get a pretty good approximation via something like the following
(warning: untested).

--------
struct yes_type { char _dummy[1]; };
struct no_type { char _dummy[2]; };

struct dummy_result_t { };
yes_type is_dummy_result(dummy_result_t);
no_type is_dummy_result(...);

struct base_t
{ void xxx() { } };
template< void (base_t::*)( ) >
struct has_mem_fn_detector
{ typedef no_type type; };

template< class T >
struct derived_t : T, base_t
{
    using T::xxx;
    dummy_result_t xxx(...) const;
};

template< class T >
typename has_mem_fn_detector< &T::xxx >::type
has_mem_fn_test(int);
template< class T >
yes_type
has_mem_fn_test(...);
--------

First, you determine if T *has* a member function called xxx, regardless of
signature, via

struct derived2_t : T, base_t { };
sizeof( has_mem_fn_test< derived2_t >(0) ) == sizeof( yes_type )

and, if this passes, then you use the expression declval< derived_t<T>
>().xxx(arg0, arg1) (for example) to query, first, whether the type of the
expression is dummy_result_t; then, assuming not, whether the type of the
expression has the convertibility properties you desire (for example).

It's not perfect, e.g., it breaks when xxx is private or if you want to
check nullary member functions (must resort to exact signatures in that
case, I think), and I haven't tested what happens when xxx is a static
member function. But I don't think any of this is using SFINAE for
expressions.

- Jeff


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