
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.

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 */ #include <boost/mpl/assert.hpp> #include <boost/type_traits/is_same.hpp> struct test1{typedef char type;}; struct test2{typedef char tope;}; typedef char test3; int main() { BOOST_MPL_ASSERT((boost::is_same<nested_type<test1>::type,char>)); BOOST_MPL_ASSERT((boost::is_same<nested_type<test2>::type,test2>)); BOOST_MPL_ASSERT((boost::is_same<nested_type<test3>::type,test3>)); } Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

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

Steven Watanabe <watanabesj <at> gmail.com> writes:
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. [...] Something like this could do: [...] 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> > {};
Oh, yes, that's definitely the way to go, didn't remember about BOOST_MPL_HAS_XXX_TRAIT_DEF. Sorry for the noise. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (3)
-
er
-
Joaquin M Lopez Munoz
-
Steven Watanabe