Hi, I haven't found any discussion of this issue; it's possible I've just missed it. Anyway, the short form is that I'm trying to partially specialize a class for tuple types. I specialize for cons<H, T>, but specialization requires an exact type match, so tuple<...> will fail to match cons<...> and instead fall to the base template<class T> case--which has the wrong behavior. If I assume that the passed type is a tuple, I can use inheritance trickery and inherit from the tuple<...>::inherited case to match the correct case. Of course, this assumes that the template type is a tuple, when I can't make that assumption--I'm using specialization precisely because the type isn't known. I could specialize on tuple<T0...T9>, but that sets another arbitrary max tuple size; it would be nice to be able to match all tuples allowed by the boost::tuple implementation. 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. Of course, it's possible I'm just missing a simple way to partially specialize for tuple types, or that such functionality is bad and unecessary. Thoughts? ---- Bryan Silverthorn :: bcs26@cornell.edu
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
participants (2)
-
Bryan Silverthorn
-
Douglas Gregor