c++ – Type decision based on existence of nested typedef – Education Career Blog

I need to define a template struct such that:

element<T>::type

is of type:

T::element_type 

if T contains a (public) typedef named element_type, otherwise (if it does not contain such typedef)

element<T>::type

is of type

T::value_type 

if T is mutable and of type

const T::value_type

if T is constant.

I am really struggling with this, any suggestion is very appreciated! 🙂

Thank you very much for your help in advance!

,

Maybe something like:

template <typename T>
struct has_element_type
{
    typedef char yes1;
    typedef char no2;

    template <typename C>
    static yes& test(typename C::element_type*);

    template <typename>
    static no& test(...);

    static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};

template <typename T>
struct is_const
{
    static const bool value = false;
};


template <typename T>
struct is_const<const T>
{
    static const bool value = true;
};

template <typename, bool> // true -> const
struct value_type_switch; 

template <typename T>
struct value_type_switch<T, true>
{
    typedef const typename T::value_type type;
};

template <typename T>
struct value_type_switch<T, false>
{
    typedef typename T::value_type type;
};

template <typename, bool> // true -> has element_type
struct element_type_switch;

template <typename T>
struct element_type_switch<T, true>
{
    typedef typename T::element_type type;
};


template <typename T>
struct element_type_switch<T, false>
{
    typedef typename value_type_switch<T, is_const<T>::value>::type type;
};

template <typename T>
struct element
{
    typedef typename element_type_switch<T,
                                    has_element_type<T>::value>::type type;
};

This should of course be split up and organized.

,

As an alternative to declaring a traits class using SFINAE, you can use it more subtly with partial specialization.

template< typename T >
struct empty { // support class is like stripped-down enable_if
    typedef void type;
};

template< class T, typename v = void > // v is always void!
struct element {
    typedef typename T::value_type type;
};

template< class T, typename v >
struct element< T const, v > {
    typedef typename T::value_type const type;
};

template< class T > // T in deduced context, T::element_type is SFINAE:
struct element< T, typename empty< typename T::element_type >::type > {
    typedef typename T::element_type type;
};

… you might want to add another case to make element_type const for const T? Unfortunately this doesn’t work in GCC, although Comeau accepts it.

template< class T >
struct element< T const, typename empty< typename T::element_type >::type > {
    typedef typename T::element_type const type;
};

Code I used to test this:

struct has_et {
    typedef int element_type;
};

struct has_vt {
    typedef char value_type;
};

char c;
int i;

element<has_vt>::type *cp = &c;
element<has_et>::type *ip = &i;

Leave a Comment