Boost logo

Boost Users :

Subject: Re: [Boost-users] [EnableIf?] Seeking an example....
From: Joel Falcou (joel.falcou_at_[hidden])
Date: 2008-09-17 12:00:06


Mathias Gaunard a écrit :
> Robert Jones wrote:
>> Somewhere in the Boost headers, or maybe the Boost documentation I
>> have seen a
>> bit of code which uses SFINAE, and also possibly the enable_if header
>> to select either
>> a find class method or find global template depending on what's
>> available.
That's something that was already explained indeed.
Here is a way to do it. Let's say you want to test for presence of a
go() method
with the following prototype :

void go();

in any type :

typedef char NotFound; struct Found { char x[2]; };

template <class T, void (T::*)() const > struct test_for_member_go{ };
template<class T> static Found
test_for_go(test_for_member_go<T,&T::go>*);
template<class T> static NotFound test_for_go( ... );

template<class T>
struct test
{
  static const bool value = (sizeof( test_for_go<T>( 0 ) ) ==
sizeof(Found));
 typedef boost::mpl::bool_<value> type;
};

I *think* it can be made in a macro using BOOST_TYPEOF and some __LINE__
shenanigans to generate this for any method of any prototype.


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