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의 컨셉츠 이전에 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 이슈 1558 (C++11 결함)이 해결되기 전까지, alias templates 에서 사용되지 않는 매개변수는 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을 값 타입으로 가지는 반복자 특성,
// 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)
오버로드 해결에서 함수 오버로드나 템플릿 특수화를 조건부로 제거 합니다
(클래스 템플릿)