Namespaces
Variants

std:: void_t

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

(C++11)
void_t
(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
定义于头文件 <type_traits>
template < class ... >
using void_t = void ;
(C++17 起)

将任意类型序列映射到 void 类型的元函数工具。该元函数是在 C++20 的 concepts 出现前利用 SFINAE 的便捷方式,特别适用于根据表达式在 未求值上下文 (如作为 decltype 表达式操作数)中的有效性,有条件地从 候选集 中移除函数,从而允许基于支持的操作存在单独的函数重载或特化版本。

注释

该元函数用于模板元编程中,在SFINAE上下文中检测不合规类型:

// 主模板处理没有嵌套 ::type 成员的类型:
template<class, class = void>
struct has_type_member : std::false_type {};
// 特化识别具有嵌套 ::type 成员的类型:
template<class T>
struct has_type_member<T, std::void_t<typename T::type>> : std::true_type {};

它也可用于检测表达式的有效性:

// 主模板处理不支持前置递增的类型:
template<class, class = void>
struct has_pre_increment_member : std::false_type {};
// 特化识别支持前置递增的类型:
template<class T>
struct has_pre_increment_member<T,
           std::void_t<decltype(++std::declval<T&>())>
       > : std::true_type {};

CWG issue 1558 (一个 C++11 缺陷)得到解决之前, 别名模板 中未使用的参数不能保证支持 SFINAE 且可能被忽略,因此早期编译器需要更复杂的 void_t 定义,例如

template<typename... Ts>
struct make_void { typedef void type; };
template<typename... Ts>
using void_t = typename make_void<Ts...>::type;
功能测试 标准 功能
__cpp_lib_void_t 201411L (C++17) std::void_t

示例

#include <iomanip>
#include <iostream>
#include <map>
#include <type_traits>
#include <vector>
// 检查类型是否具有 begin() 和 end() 成员函数的变量模板
template<typename, typename = void>
constexpr bool is_range = false;
template<typename T>
constexpr bool is_range<T,
    std::void_t<decltype(std::declval<T>().begin()),
                decltype(std::declval<T>().end())>> = true;
// 迭代器特征类,其 value_type 是被迭代容器的 value_type,
// 甚至支持 back_insert_iterator(其 value_type 为 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_range<std::vector<double>>);
    SHOW(is_range<std::map<int, double>>);
    SHOW(is_range<double>);
    SHOW(is_range<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>);
}

输出:

is_range<std::vector<double>>   == true
is_range<std::map<int, double>> == true
is_range<double>                == false
is_range<A>                     == false

参见

(C++11)
有条件地 移除 函数重载或模板特化,使其不参与重载决议
(类模板)