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
.
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;
Examples
See also
(C++11) | hides a function overload or template specialization based on compile-time boolean (class template) |
Please login to continue.