std::void_t(3)
π NAME π
<pink>okay so</pink> bestie, this is std::void_t β std::void_t. Thatβs the name, no cap. Youβre on the std::void_t(3) manual page from the C++ Standard Library, and itβs honestly a lil template-metaprogramming icon π
.
β¨ Synopsis β¨
Defined in header <type_traits>:
template< class... > (since C++17)
using void_t = void;
Utility metafunction that maps a sequence of any types to the type void. Itβs a convenient way to leverage SFINAE prior to C++20βs concepts, especially for conditionally removing functions from the candidate set based on whether an expression is valid in the unevaluated context (such as an operand to a decltype expression). That lets separate function overloads or specializations exist based on supported operations. slay β¨
π§ Notes π§
This metafunction is used in template metaprogramming to detect ill-formed types in a SFINAE context:
// primary template handles types that have no nested ::type member:
template<class, class = void>
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 = void>
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 the resolution of CWG issue 1558 (a C++11 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;
π¬ Feature-test macro π¬
__cpp_lib_void_tβ value:201411L; std:(C++17); feature:std::void_t
π Example π
// Run this code
#include <iomanip>
#include <iostream>
#include <map>
#include <type_traits>
#include <vector>
// Variable template that checks if a type has begin() and end() member functions
template<typename, typename = void>
constexpr bool is_iterable = false;
template<typename T>
constexpr bool is_iterable<
T,
std::void_t<decltype(std::declval<T>().begin()),
decltype(std::declval<T>().end())
>
> = true;
// An iterator trait those value_type is the value_type of the iterated container,
// supports even back_insert_iterator (where 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> {};
class A {};
#define SHOW(...) std::cout << std::setw(34) << #__VA_ARGS__ \
<< " == " << __VA_ARGS__ << '\n'
int main()
{
std::cout << std::boolalpha << std::left;
SHOW(is_iterable<std::vector<double>>);
SHOW(is_iterable<std::map<int, double>>);
SHOW(is_iterable<double>);
SHOW(is_iterable<A>);
using container_t = std::vector<int>;
container_t v;
static_assert(std::is_same_v<
container_t::value_type,
iterator_trait<decltype(std::begin(v))>::value_type
>);
static_assert(std::is_same_v<
container_t::value_type,
iterator_trait<decltype(std::back_inserter(v))>::value_type
>);
}
π» Output π»
is_iterable<std::vector<double>> == true
is_iterable<std::map<int, double>> == true
is_iterable<double> == false
is_iterable<A> == false
π See also π
enable_ifβ conditionally removes a function overload or template specialization from overload resolution.(C++11)(class template)
mwah π β source: http://cppreference.com, version/date: 2024.06.10, manual page: std::void_t(3). periodt π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms