Namespaces
Variants

std:: conjunction

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
conjunction
(C++17)
(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)

Compile-time rational arithmetic
Compile-time integer sequences
헤더에 정의됨 <type_traits>
template < class ... B >
struct conjunction ;
(C++17부터)

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

특수화 std :: conjunction < B1, ..., BN > 는 다음과 같은 public이고 모호하지 않은 기반 클래스를 가집니다

  • 만약 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 의 인스턴스화를 요구하지 않습니다.

프로그램이 std::conjunction 이나 std::conjunction_v 에 대한 특수화를 추가하는 경우, 그 동작은 정의되지 않습니다.

목차

템플릿 매개변수

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

헬퍼 변수 템플릿

template < class ... B >
constexpr bool conjunction_v = conjunction < B... > :: value ;
(C++17부터)

가능한 구현

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 에서 상속받지 않습니다: 단순히 ::value bool 로 명시적 변환 시 false 가 되는 첫 번째 B 에서 상속받거나, 모든 B true 로 변환될 때는 마지막 B 에서 상속받습니다. 예를 들어, std :: conjunction < std:: integral_constant < int , 2 > , std:: integral_constant < int , 4 >> :: value 4 입니다.

단락 평가 인스턴스화는 conjunction 폴드 표현식 과 구별합니다: ( ... && Bs :: value ) 와 같은 폴드 표현식은 Bs 에 있는 모든 B 를 인스턴스화하는 반면, std :: conjunction_v < Bs... > 는 값이 결정될 수 있는 시점에서 인스턴스화를 중단합니다. 이는 이후 타입의 인스턴스화 비용이 높거나 잘못된 타입으로 인스턴스화될 경우 심각한 오류를 발생시킬 수 있는 경우에 특히 유용합니다.

기능 테스트 매크로 표준 기능
__cpp_lib_logical_traits 201510L (C++17) 논리 연산자 타입 특성

예제

#include <iostream>
#include <type_traits>
// func is enabled if all Ts... have the same type as T
template<typename T, typename... Ts>
std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...>>
func(T, Ts...)
{
    std::cout << "All types in pack are the same.\n";
}
// otherwise
template<typename T, typename... Ts>
std::enable_if_t<!std::conjunction_v<std::is_same<T, Ts>...>>
func(T, Ts...)
{
    std::cout << "Not all types in pack are the same.\n";
}
template<typename T, typename... Ts>
constexpr bool all_types_are_same = std::conjunction_v<std::is_same<T, Ts>...>;
static_assert(all_types_are_same<int, int, int>);
static_assert(not all_types_are_same<int, int&, int>);
int main()
{
    func(1, 2, 3);
    func(1, 2, "hello!");
}

출력:

All types in pack are the same.
Not all types in pack are the same.

참고 항목

(C++17)
논리 NOT 메타함수
(클래스 템플릿)
가변 논리 OR 메타함수
(클래스 템플릿)