
Boost : 
From: David Abrahams (dave_at_[hidden])
Date: 20031218 14:15:09
"Jonathan Turkanis" <technews_at_[hidden]> writes:
> "David Abrahams" <dave_at_[hidden]> wrote in message
> news:u3cbigbr0.fsf_at_boostconsulting.com...
>> "Jonathan Turkanis" <technews_at_[hidden]> writes:
>>
>> > I have occassionally used mpl::bool_ as a tool for compiletime dispatch
>> > based on a boolean condition. E.g.:
>> >
>> > template<typename T>
>> > void f(const T&, mpl::true_) { /* T is integral */ }
>> >
>> > template<typename T>
>> > void f(const T&, boost::mpl::false_) { /* T is nonintegral */ }
>> >
>> > template<typename T>
>> > void f(const T& t) { f(t, mpl::bool_<is_integral<T>::value>()); }
>>
>> Better:
>>
>> template<typename T>
>> void f(const T& t) { f(t, typename is_integral<T>::type()); }
>>
>
> Thanks. Looking into the guts of the type traits library, is see why this
> works. (BTW, I also see why lambda expressions based on TT work on compilers
> without template template arguments or partial sepcialzationby cheating!
> Should I cheat too when I define traits templates?)
Yes, by using BOOST_MPL_AUX_LAMBDA_SUPPORT. See
libs/python/detail/indirect_traits.hpp for a few examples.
> But is_integral was just an example. For a general MPLcompatible boolean
> type traits template, the only guarantee is that the inner 'type' is a model
> of intergral constant with value_type bool., right?
Well, this is kind of subtle. There's no defined concept "boolean
type traits template". A general MPLcompatible metafunction is a
class or a class template with a publiclyaccessible nested type.
You know what Integral Constant looks like, so you can put them
together to get a set of expectations.
> Doesn't this mean that
> is_xxx<T>::type and is_xxx<S>::type need not be the same, even if T and S
> both saitisfy or both fail to satify the condition?
In general? Yes, it means that. You can only use tricks like the
above if you know something more about the metafunction in question.
HTH,
 Dave Abrahams Boost Consulting www.boostconsulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk