Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/types/disjunction"

From cppreference.com
< cpp‎ | types
m (= in template)
(Not necessarily an integral_constant)
Line 29: Line 29:
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
 
{{cpp/types/integral_constant/inherit | if at least one {{tt|B::value}} is {{c|true}} }}
 
  
 
===Possible implementation===
 
===Possible implementation===

Revision as of 09:20, 28 October 2015

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <type_traits>
template<class... B>
struct disjunction;
(1) (since C++17)

Forms the logical disjunction of the type traits B... .

The BaseCharacteristic of a specialization std::disjunction <B1, ..., BN> is the first Bi for which Bi::value != false, or if every Bi::value == false, the BaseCharacteristic is BN.

If sizeof...(B) == 0, the BaseCharacteristic is std::false_type.

Disjunction is short-circuiting: if there is a template type argument Bi with Bi::value != false, then instantiating disjunction<B1, ..., BN>::value does not require the instantiation of Bj::value for j > i

Contents

Template parameters

B... - every type must be usable as a base class and define member B::value that is convertible to bool

Helper variable template

template<class... B>
constexpr bool disjunction_v = disjunction<B...>::value;
(since C++17)

Possible implementation

template<class...> struct disjunction; // not defined
template<> struct disjunction<> : std::false_type { };
template<class B1> struct disjunction<B1> : B1 { };
template<class B1, class B2>
struct disjunction<B1, B2> : std::conditional_t<B1::value, B1, B2>  { };
template<class B1, class B2, class B3, class... Bn>
struct disjunction<B1, B2, B3, Bn...> : std::conditional_t<B1::value, B1,
                                           disjunction<B2, B3, Bn...>> { };

Notes

A specialization of disjunction does not necessarily have a BaseCharacteristic of either std::true_type or std::false_type: it simply inherits the base characteristic of the first B whose ::value, converted to bool, is true, or the base characteristic of the very last B when all of them convert to false. For example, std::disjunction<std::integral_constant<int, 2>, std::integral_constant<int, 4>>::value is 2.

Example

See also

(C++17)
logical NOT metafunction
(class template) [edit]
variadic logical AND metafunction
(class template) [edit]