|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2003-12-18 14:15:09
"Jonathan Turkanis" <technews_at_[hidden]> writes:
> "David Abrahams" <dave_at_[hidden]> wrote in message
> news:u3cbigbr0.fsf_at_boost-consulting.com...
>> "Jonathan Turkanis" <technews_at_[hidden]> writes:
>>
>> > I have occassionally used mpl::bool_ as a tool for compile-time 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 non-integral */ }
>> >
>> > 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 sepcialzation--by 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 MPL-compatible 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 MPL-compatible metafunction is a
class or a class template with a publicly-accessible 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.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk