Boost logo

Boost Users :

Subject: [Boost-users] Using SFINAE to detect presence of member function - Unreliable
From: Naik, Roshan (roshan.naik_at_[hidden])
Date: 2009-01-30 07:45:45


    I have taken a look at anything I could find through Boost lists and Google that came up as
a solution to the problem of detecting the existence of a member function in
a type... using SFINAE.

All of them have one problem.. they are unable to detect the existence of
the member function if it was defined in a base class. They can only detect
if the member function was directly defined in the class being tested.

At their heart, all solutions boil down to some variation of the following
template class with a template parameter that is a (non-type) pointer to
member function:

template<class T, void (T::*)(void) >
struct Tester;

Then we try to SFINAE on it by the usual sizeof trick.

However the problem is that if method 'void foo(int)' is defined in a Base
class. Then the type of &Derived::foo is actually void (Base::*)(int) ...
and not void (Derived::*)(int)

Although during argument passing the conversion of the Base member function
pointer to Derived member function pointer is acceptable to C++, it will
NOT do such conversion when instantiating templates.
So trying to use Tester<Derived, &Derived::foo> will always fail us if 'foo'
is defined in Base.

So .. my question is ....does anyone have solution that works for the case
when the method is defined in the base class too ?

I am thinking ... if it is possible to extract the class name from the
actual type of &Derived::foo, then the problem can be solved... but the type
extraction mechanism must not error out on types that have absolutley no
member function called foo.

I know type erasure (which i prefer to call External polymorhism) can solve this issue
but SFINAE is more efficient at runtime.
-Roshan


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net