gpt4 book ai didi

c++ - 否定和德摩根定律不是 C++20 部分按约束排序的一部分

转载 作者:行者123 更新时间:2023-12-01 14:04:27 24 4
gpt4 key购买 nike

partial ordering by constraints的规则指 AND 和 OR,但不指 NOT:

13.5.4 Partial ordering by constraints [temp.constr.order]
(1.2) ...
- The constraint A ∧ B subsumes A, but A does not subsume A ∧ B.
- The constraint A subsumes A ∨ B, but A ∨ B does not subsume A.


这些规则基于 atomic constraints 的定义。和 constraints normalization :

13.5.3 Constraint normalization [temp.constr.normal]
1 The normal form of an expression E is a constraint that is defined
as follows:
(1.1) The normal form of an expression ( E ) is the normal form of E.
(1.2) The normal form of an expression E1 || E2 is the disjunction
of the normal forms of E1 and E2.
(1.3) The normal form of an expression E1 && E2 is the conjunction
of the normal forms of E1 and E2.


否定(即!E1)没有特别处理。

因此,以下代码正确使用了偏序:
void foo(auto i) requires std::integral<decltype(i)> {
std::cout << "integral 1" << std::endl;
}

void foo(auto i) requires std::integral<decltype(i)> && true {
std::cout << "integral 2" << std::endl;
}

int main() {
foo(0); // integral 2
}

虽然此代码因模棱两可而失败:
template<typename T>
concept not_integral = !std::integral<T>;

template<typename T>
concept not_not_integral = !not_integral<T>;

void foo(auto i) requires not_not_integral<decltype(i)> {
std::cout << "integral 1" << std::endl;
}

void foo(auto i) requires std::integral<decltype(i)> && true {
std::cout << "integral 2" << std::endl;
}

int main() {
foo(0);
}

代码: https://godbolt.org/z/RYjqr2

以上导致德摩根定律不适用于概念:
template<class P>
concept has_field_moo_but_not_foo
= has_field_moo<P> && !has_field_foo<P>;

不等于:
template<class P>
concept has_field_moo_but_not_foo
= !(has_field_foo<P> || !has_field_moo<P>);

第一个将参与部分排序,而后者则不参与。

代码: https://godbolt.org/z/aRhmyy

不将否定处理作为约束规范化的一部分的决定是否是为了简化编译器供应商的实现?或者试图支持它是否存在逻辑缺陷?

最佳答案

Was the decision, not to handle negation as part of constraint normalization, taken in order to ease the implementation for compiler vendors?



是的。这概括为在编译器中需要 SAT 求解器。

[temp.constr.op]/5 中添加了一个示例为了证明这一点,虽然它没有提供决定的理由:

template <class T> concept sad = false;

template <class T> int f1(T) requires (!sad<T>);
template <class T> int f1(T) requires (!sad<T>) && true;
int i1 = f1(42); // ambiguous, !sad<T> atomic constraint expressions ([temp.constr.atomic])
// are not formed from the same expression

template <class T> concept not_sad = !sad<T>;
template <class T> int f2(T) requires not_sad<T>;
template <class T> int f2(T) requires not_sad<T> && true;
int i2 = f2(42); // OK, !sad<T> atomic constraint expressions both come from not_­sad

template <class T> int f3(T) requires (!sad<typename T::type>);
int i3 = f3(42); // error: associated constraints not satisfied due to substitution failure

template <class T> concept sad_nested_type = sad<typename T::type>;
template <class T> int f4(T) requires (!sad_nested_type<T>);
int i4 = f4(42); // OK, substitution failure contained within sad_­nested_­type


特别注意 f3之间的区别和 f4 .是否 requires !sad<typename T::type>意味着没有 sad嵌套类型,或者存在不是 sad 的嵌套类型?它实际上意味着后者,而对 f4 的约束指前者。

关于c++ - 否定和德摩根定律不是 C++20 部分按约束排序的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60453865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com