Boost logo

Boost Users :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2003-03-23 12:45:23


On Thursday 20 March 2003 01:50 pm, Bryan Silverthorn wrote:
> So: I need a way to determine if a particular type is a tuple type,
> which would allow me to do the following:
>
> template<bool B, class T>
> class FooIf {};
> template<class T>
> class Foo : public FooIf<is_tuple_type<T>, T> {};
> template<class T>
> class FooIf<true, T> { /* tuple implementation */ };
> template<class T>
> class FooIf<false, T> { /* non-tuple implementation */ };
>
> AFAICT, there's no way to do so at present. I'd like to suggest a way to
> add this functionality: have cons<> (or tuple<>) inherit from a blank
> "tuple_base" class. This would let us use boost::is_base_and_derived to
> provide the is_tuple_type functionality for the code block above.

You can determine if a type T is a cons<...> or has a cons<...> base with:

namespace boost { namespace tuples {

template<typename H, typename T>
type_traits::yes_type is_cons_or_derived_helper(cons<H, T> const volatile*);

type_traits::no_type is_cons_or_derived_helper(void const volatile*);

template<typename T>
struct is_cons_or_derived
{
  BOOST_STATIC_CONSTANT(bool, value =
   (sizeof(is_cons_or_derived_helper((T*)0)) ==
sizeof(type_traits::yes_type));
};

} }

        Doug


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