简体   繁体   中英

Using SFINAE and void_t to determine type of elements inside a container

In the code below, I'm trying to determine the type of elements inside a container by checking whether a container C has member value_type . If true, I set the type to "value_type". However even when a type doesn't have member value_type and isn't a container, when passed, compiler seems to set the second argument of HasMemberT_value_type to True, even though it gives error.

template<typename...>
using void_t = void;

template<typename T, typename = void_t<>>
struct HasMemberT_value_type : std::false_type
{
};

template<typename T>
struct HasMemberT_value_type<T, void_t<typename T::value_type>> : std::true_type
{
};

template<typename T, bool = HasMemberT_value_type<T>::value>
struct ElementTypeT
{
    using Type = typename T::value_type;
};

template<typename T>
struct ElementTypeT<T, false>
{
};

template<typename T>
using ElementType = typename ElementTypeT<T>::Type;

template<typename T>
void printType(T const& c)
{
    std::cout << "Container of " << typeid(ElementType<T>).name() << " elements.\n";
}

int main()
{                                           
    std::array<char, 5> arr;
    char classic[] = {'a', 'b', 'c', 'd'};
                                            //GNU Compiler:
    printType<arr>();                       //Container of c elements.
    printType<classic>();                   //ERROR : "In instantiation of ‘struct ElementTypeT<char [4], true>’: ... error: ‘char [4]’ is not a
                                            //         class, struct, or union type
                                            //         using Type = typename T::value_type;"

}

In instantiation of 'struct ElementTypeT<char [4], true>

why is it set to true??

Thank you.

printType<arr>() and printType<classic>() won't compile. It should be printType(arr) and printType(classic) .

The other problem is that ElementTypeT<T, true> has a Type member, but ElementTypeT<T, false> does not. So when you do using ElementType = typename ElementTypeT<T>::Type and access that when you do printType(classic) , it will fail.

To fix this, modify the specialization so that the array can be deduced:

template<typename T, std::size_t I>
struct ElementTypeT<T[I], false>
{
    using Type=T;
};

Not sure why ElementTypeT<char [4], true> is instaniating in your code. When I ran it it came up false for me.

Here's a simpler way to do this using function overloading and SFINAE:

template<class T>
typename std::decay_t<T>::value_type get_value_type( T&& );

template<class R, std::size_t I>
R get_value_type( R(&)[I] );

template<class T>
void printType(T const& c) {
    std::cout << "Container of " << typeid(get_value_type(c)).name() << " elements.\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM