##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::void_t

From cppreference.com
< cpp‎ | types

Defined in header `<type_traits>` |  |
template< class... > using void_t = void; |  | (since C++17)

Utility metafunction that maps a sequence of any types to the type void

### [edit] Notes

This metafunction is used in template metaprogramming to detect ill-formed types in SFINAE context:

    // primary template handles types that have no nested ::type member:
    template< class, class = std::void_t<> >
    struct has_type_member : std::false_type { };

    // specialization recognizes types that do have a nested ::type member:
    template< class T >
    struct has_type_member<T, std::void_t<typename T::type>> : std::true_type { };

It can also be used to detect validity of an expression:

    // primary template handles types that do not support pre-increment:
    template< class, class = std::void_t<> >
    struct has_pre_increment_member : std::false_type { };
    // specialization recognizes types that do support pre-increment:
    template< class T >
    struct has_pre_increment_member<T,
               std::void_t<decltype( ++std::declval<T&>() )>
           > : std::true_type { };

Until CWG 1558 (a C++14 defect), unused parameters in alias templates were not guaranteed to ensure SFINAE and could be ignored, so earlier compilers require a more complex definition of `void_t`, such as

    template<typename... Ts> struct make_void { typedef void type;};
    template<typename... Ts> using void_t = typename make_void<Ts...>::type;

### [edit] Examples

Run this code

    #include <iostream>
    #include <type_traits>
    #include <vector>
    #include <map>

    class A {};

    template <typename T, typename = void>
    struct is_iterable : std::false_type {};
    template <typename T>
    struct is_iterable<T, std::void_t<decltype(std::declval<T>().begin()),
                                      decltype(std::declval<T>().end())>>
        : std::true_type {};

    int main()
    {
        std::cout << std::boolalpha;
        std::cout << is_iterable<std::vector<double>>::value << '\n';
        std::cout << is_iterable<std::map<int, double>>::value << '\n';
        std::cout << is_iterable<double>::value << '\n';
        std::cout << is_iterable<A>::value << '\n';
    }

Output:

    true
    true
    false
    false

### [edit] See also

enable_if (C++11) | hides a function overload or template specialization based on compile-time boolean (class template) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/types/void_t&oldid=89901"