首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::conjunction

Defined in header <type_traits>

?

?

template<class... B> struct conjunction;

(1)

(since C++17)

形成逻辑连接类型性状B...,有效地执行一个逻辑和一系列的特性。

专业化std::conjunction<B1, ..., BN>有一个公共的和明确的基础,也就是说。

  • 如果sizeof...(B) == 0,,,std::true_type;否则
  • 第一类BiB1, ..., BN对此bool(Bi::value) == false,或BN如果没有这样的类型。

基类的成员名称,但conjunctionoperator=,则不隐藏,并且可以在conjunction...

连接是短路的:如果有模板类型参数。Bi带着bool(Bi::value) == false,然后实例化conjunction<B1, ..., BN>::value不需要实例化Bj::valuej > i...

模板参数

B...

-

every template argument Bi for which Bi::value is instantiated must be usable as a base class and define member value that is convertible to bool

辅助变量模板

template<class... B> inline constexpr bool conjunction_v = conjunction<B...>::value;

?

(since C++17)

可能的实施

模板<class...>结构连接:std::true[医]类型{};模板<class B1>结构连接<B1>:B1{};模板<类B1,类...。结构连接<B1,Bn...>:std::条件[医]t<bool%28B1::值%29,连词<Bn.>,B1>{};

*。

注记

...的专业化conjunction不一定继承std::true_typestd::false_type*它只是继承了第一个B谁的::value,显式转换为bool,是false,还是从最后一个B当他们都变成真的时候。例如,std::conjunction<std::integral_constant<int, 2>,std::integral_constant<int, 4>>::value4...

短路实例化conjunction从折叠表达式:折叠表达式,如(... && Bs::value)实例化每个BBs,同时std::conjunction_v<Bs...>一旦确定了值,就停止实例化。如果后面的类型实例化成本很高,或者使用错误的类型实例化会导致硬错误,则这尤其有用。

二次

代码语言:javascript
复制
#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 T\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 T\n";
}
 
int main() {
    func(1, 2, 3);
    func(1, 2, "hello!");
}

二次

产出:

二次

代码语言:javascript
复制
all types in pack are T
not all types in pack are T

二次

另见

negation (C++17)

logical NOT metafunction (class template)

disjunction (C++17)

variadic logical OR metafunction (class template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com