命名空间
变体
操作

std::void_t

来自 cppreference.cn
< cpp‎ | types
 
 
元编程库
类型特征
类型类别
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
类型属性
(C++11)
(C++11)
(C++14)
(C++11)(在 C++26 中弃用)
(C++11)(直到 C++20*)
(C++11)(在 C++20 中弃用)
(C++11)
类型特征常量
元函数
(C++17)
支持的操作
关系和属性查询
类型修改
(C++11)(C++11)(C++11)
类型转换
(C++11)(在 C++23 中弃用)
(C++11)(在 C++23 中弃用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
void_t
(C++17)
编译时有理算术
编译时整数序列
 
定义于头文件 <type_traits>
template< class... >
using void_t = void;
(自 C++17 起)

实用元函数,将任何类型的序列映射到类型 void。此元函数是在 C++20 的 概念 之前利用 SFINAE 的便捷方式,尤其用于根据表达式在未求值语境(例如 decltype 表达式的操作数)中是否有效来有条件地从候选集中移除函数,从而允许基于支持的操作存在单独的函数重载或特化。

[编辑] 注释

此元函数用于模板元编程,以检测 SFINAE 上下文中的病构类型

// 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 {};

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

// 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 {};

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;
特性测试 Std 特性
__cpp_lib_void_t 201411L (C++17) std::void_t

[编辑] 示例

#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
    >);
}

输出

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

[编辑] 参见

(C++11)
有条件地从重载决议中移除函数重载或模板特化
(类模板) [编辑]