Boost logo

Boost Users :

Subject: Re: [Boost-users] Nullary metafunction
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-02-03 16:08:31


AMDG

Joaquin M Lopez Munoz wrote:
> er <erwann.rogard <at> gmail.com> writes:
>
>
>> Hi All,
>>
>> I'm looking for template<typename> class F such that
>>
>> F<T>::type is T::type if it exists, T itself otherwise.
>>
>> I was hoping apply<T>::type would work, by analogy with the well known
>> example apply<std::vector<_>,X>::type, but apparently not.
>>
>>
>
> Something like this could do:
>
> #include <boost/mpl/if.hpp>
>
> namespace nested_type_detail{
> typedef char yes;
> struct no{char x[2];};
> template<typename T>
> yes helper(T*,typename T::type* =0);
> no helper(...);
> }
> template<typename T>
> struct nested_type
> {
> struct fallback{typedef T type;};
> typedef typename boost::mpl::if_c<
> sizeof(nested_type_detail::helper(static_cast<T*>(0)))==
> sizeof(nested_type_detail::yes),
> T,
> fallback
> >::type::type type;
> };
>
> /* testing */
>

Wouldn't it be easier to use the MPL components
that already exist?

BOOST_MPL_HAS_XXX_TRAIT_DEF(type);

template<class T>
struct nested_type :
  boost::mpl::eval_if<has_type<T>,
    T,
    boost::mpl::identity<T> >
{};

In Christ,
Steven Watanabe


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