types_void_t
types_void_t (임의 타입 시퀀스를 void로 매핑하는 유틸리티 메타함수)
std::void_t는 임의의 타입 시퀀스를 void 타입으로 매핑하는 유틸리티 메타함수예요. C++20의 concepts 이전에 SFINAE를 활용하는 편리한 방법을 제공하며, 표현식이 유효한지에 따라 함수 오버로드나 특수화를 조건부로 활성화할 수 있게 해줘요.
출처: cppreference
본문
| <type_traits> 헤더에 정의됨 | ||
|---|---|---|
| template < class ... > using void_t = void ; | (C++17부터) |
임의의 타입 시퀀스를 void 타입으로 매핑하는 유틸리티 메타함수예요. 이 메타함수는 C++20의 concepts 이전에 SFINAE를 활용하는 편리한 방법으로, 특히 비평가 문맥(unevaluated context, 예: decltype 표현식의 피연산자)에서 표현식이 유효한지에 따라 후보 집합에서 함수를 조건부로 제거할 수 있게 해줘요. 이를 통해 지원되는 연산에 따라 별도의 함수 오버로드나 특수화를 존재하게 할 수 있어요.
Notes (참고 사항)
이 메타함수는 템플릿 메타프로그래밍에서 SFINAE 문맥의 ill-formed 타입을 감지하는 데 사용돼요:
// 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;
| Feature-test 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
| __cpp_lib_void_t | 201411L | (C++17) | std::void_t |
Example (예제)
#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_range = false;
template<typename T>
constexpr bool is_range<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_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
See also (함께 보기)
| enable_if (C++11) | 조건부로 함수 오버로드나 템플릿 특수화를 오버로드 해석에서 제거해요 (클래스 템플릿) [edit] |
|---|