按约束进行偏序的规则是指 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.
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? or is there a logical flaw with trying to support it?