Namespaces
Variants
Views
Actions

std::void_t

From cppreference.com
< cpp‎ | types
 
 
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(deprecated in C++17)
(C++11)
Type trait constants
Metafunctions
(C++17)
Endian
(C++20)
Supported operations
Relationships and property queries
(C++11)
(C++11)
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
void_t
(C++17)
(C++11)(deprecated in C++17)(C++17)
 
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

#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 {};
 
// An iterator trait which value_type is always the value_type of the 
// iterated container, even with back_insert_iterator which value_type is void
 
template <typename T, typename = void>
struct iterator_trait 
: std::iterator_traits<T> {};
template <typename T>
struct iterator_trait<T, std::void_t<typename T::container_type>> 
: std::iterator_traits<typename T::container_type::iterator> {};
 
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';
 
 
    std::vector<int> v;
 
    std::cout << std::is_same<iterator_trait<decltype(std::back_inserter(v))>::value_type
    , iterator_trait<decltype(v.cbegin())>::value_type >::value << '\n';
}

Output:

true
true
false
false
true

[edit] See also

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