是否存在您无法正确处理的特定情况std::conjunction
/std::disjunction
并且不使用更“基本”(即语言功能而不是库功能)的折叠表达式&&
/||
?
Example:
// func is enabled if all Ts... have the same type
template<typename T, typename... Ts>
std::enable_if_t<std::conjunction_v<std::is_same<T, Ts>...> >
func(T, Ts...) {
// TODO something to show
}
vs
// func is enabled if all Ts... have the same type
template<typename T, typename... Ts>
std::enable_if_t<(std::is_same<T, Ts> &&...)>
func(T, Ts...) {
// TODO something to show
}
使用折叠表达式的版本更简短,通常更具可读性(尽管意见可能有所不同)。所以我不明白为什么它与折叠表达式一起添加到库中。
std::conjunction
短路::value
实例化,而折叠表达式则不然。这意味着,给定:
template <typename T>
struct valid_except_void : std::false_type { };
template <>
struct valid_except_void<void> { };
将编译以下内容:
template <typename... Ts>
constexpr auto test = std::conjunction_v<valid_except_void<Ts>...>;
constexpr auto inst = test<int, void>;
但以下情况不会:
template <typename... Ts>
constexpr auto test = (valid_except_void<Ts>::value && ...);
constexpr auto inst = test<int, void>;
godbolt.org 上的实例 https://gcc.godbolt.org/z/V99KHr
From 参考参数 https://en.cppreference.com/w/cpp/types/conjunction:
连接是短路的:如果有模板类型参数Bi
with bool(Bi::value) == false
,然后实例化conjunction<B1, ..., BN>::value
不需要实例化Bj::value
for j > i
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)