Boost logo

Boost Users :

From: Joel FALCOU (joel.falcou_at_[hidden])
Date: 2008-07-22 15:35:38


John Pretz a écrit :
> Hi,
>
> I'm pretty new to boost. I want to write some code with a
> compile-time check whether a member function exists or not. The code
> below shows kinda what I need except I'd obviously like to avoid a
> specialized template for each class that I use.
>
> Can anybody point me in the right direction?
>
Not sure if it exists in boost, but the good old trick using the (...)
operator may be enough.
Like :

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;
};

then test<X>::type should give you a CT boolean that indicates if go is
a member of T.
This could be easily turned into a macro-like features that can be
retargeted for any member with any signature.

-- 
Joel FALCOU
Research Engineer @ Institut d'Electronique Fondamentale
Université PARIS SUD XI
France 

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