Boost logo

Boost Users :

From: John Pretz (jpretz_at_[hidden])
Date: 2008-07-22 16:38:46


Thank you very much. That worked great.

John

Joel FALCOU wrote:
> 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.
>

-- 
John Pretz --  P-23, MS H803
Los Alamos National Lab  Los Alamos, NM 87545
Phone: (505)665-5847  Fax: (505)665-4121

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