Namespaces
Variants

std::experimental:: conjunction

From cppreference.net
헤더 파일에 정의됨 <experimental/type_traits>
template < class ... B >
struct conjunction ;
(라이브러리 fundamentals TS v2)

타입 특성 B... 논리곱 을 형성하며, 특성 시퀀스에 대해 논리 AND 연산을 효과적으로 수행합니다.

특수화 std :: experimental :: conjunction < B1, ..., BN > 는 공개적이고 명확한 베이스를 가집니다.

  • 만약 sizeof... ( B ) == 0 이면, std:: true_type ; 그렇지 않으면
  • B1, ..., BN 중에서 bool ( Bi :: value ) == false 인 첫 번째 타입 Bi , 또는 그러한 타입이 없으면 BN

기본 클래스의 멤버 이름 중 conjunction operator= 를 제외한 나머지는 숨겨지지 않으며, conjunction 내에서 명확하게 사용 가능합니다.

논리곱(Conjunction)은 단락 평가(short-circuiting)를 수행합니다: 만약 Bi 템플릿 타입 인자 중 bool ( Bi :: value ) == false 인 경우가 존재하면, conjunction < B1, ..., BN > :: value 의 인스턴스화는 j > i Bj :: value 의 인스턴스화를 요구하지 않습니다.

목차

템플릿 매개변수

B... - 인스턴스화되는 모든 템플릿 인수 Bi 에 대해 Bi :: value 는 베이스 클래스로 사용 가능해야 하며 bool 로 변환 가능한 멤버 value 를 정의해야 함

헬퍼 변수 템플릿

template < class ... B >
constexpr bool conjunction_v = conjunction < B... > :: value ;
(라이브러리 fundamentals TS v2)

가능한 구현

template<class...> struct conjunction : std::true_type {};
template<class B1> struct conjunction<B1> : B1 {};
template<class B1, class... Bn>
struct conjunction<B1, Bn...> 
    : std::conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};

참고 사항

conjunction 의 특수화는 반드시 std:: true_type 또는 std:: false_type 를 상속받지 않습니다: 단순히 bool로 변환된 ::value가 false인 첫 번째 B를 상속하거나, 모든 값이 true로 변환될 경우 마지막 B를 상속합니다. 예를 들어, conjunction < std:: integral_constant < int , 2 > , std:: integral_constant < int , 4 >> :: value 4 입니다.

예제

#include <experimental/type_traits>
#include <iostream>
// func는 모든 Ts...이 동일한 타입을 가질 때 활성화됨
template<typename T, typename... Ts>
constexpr std::enable_if_t<std::experimental::conjunction_v<std::is_same<T, Ts>...>>
func(T, Ts...)
{
    std::cout << "All types are the same.\n";
}
template<typename T, typename... Ts>
constexpr std::enable_if_t<!std::experimental::conjunction_v<std::is_same<T, Ts>...>>
func(T, Ts...)
{
    std::cout << "Types differ.\n";
}
int main()
{
    func(1, 2'7, 3'1);    
    func(1, 2.7, '3');    
}

출력:

All types are the same.
Types differ.

참고 항목

가변 인자 논리 AND 메타함수
(클래스 템플릿)