C++20 中 "Relation" 概念定义背后的动机
Motivation behind the definition of "Relation" concept in C++20
两者 the online C++ draft and cppreference 定义标准 Relation
概念如下:
template <class R, class T, class U>
concept Relation =
std::Predicate<R, T, T> && std::Predicate<R, U, U> &&
std::Predicate<R, T, U> && std::Predicate<R, U, T>;
这个定义让我吃惊,因为我希望看到类似
的东西
template <class R, class T, class U>
concept Relation = std::Predicate<R, T, U>;
或者可能
template <class R, class T, class U>
concept Relation = std::Predicate<R, T, U> && std::Predicate<R, U, T>;
甚至
template <class R, class T, class U>
concept Relation = std::Predicate<R, T, U> || std::Predicate<R, U, T>;
根据我的理解,类型 T
和 U
之间的 relation 是对 (T, U)
上的二元谓词。因此, 评估两个 T
类型对象或两个 U
类型对象的关系是没有意义的。但是,给定的定义要求关系应该可以用参数 (T,T)
和 (U,U)
.
调用
我的问题是:这个Relation
概念的定义(看似错误)背后的动机是什么?
cppreference 上给出的解释是
The concept Relation specifies that R defines a binary relation over the set of expressions whose type and value category are those encoded by either T or U.
(强调我的)
这对我来说听起来很奇怪:为什么一般的 Relation
概念被定义为 两种类型的两个支持参数 用于任意组合?
一种可能是这个概念用于指针和nullptr_t
的比较,以及迭代器和哨兵迭代器的比较。如果是这样,为什么这个概念被称为 Relation
,而不是更具体的东西,比如 InterComparable
?这只是用词不当吗?
用词不当。据我所知,它的存在是为了提供 StrictWeakOrder 之类的句法要求,而没有语义要求。
例如也可以考虑
template <class R, class T, class U>
concept Equivalence = std::Relation<R, T, U>;
//A relation r is an equivalence if
// - it is reflexive: for all x, r(x, x) is true;
// - it is symmetric: for all a and b, if r(a, b) is true then r(b, a) is true;
// - it is transitive: for all a, b and c, if r(a, b) and r(b, c) are both true then r(a, c) is true;
两者 the online C++ draft and cppreference 定义标准 Relation
概念如下:
template <class R, class T, class U>
concept Relation =
std::Predicate<R, T, T> && std::Predicate<R, U, U> &&
std::Predicate<R, T, U> && std::Predicate<R, U, T>;
这个定义让我吃惊,因为我希望看到类似
的东西template <class R, class T, class U>
concept Relation = std::Predicate<R, T, U>;
或者可能
template <class R, class T, class U>
concept Relation = std::Predicate<R, T, U> && std::Predicate<R, U, T>;
甚至
template <class R, class T, class U>
concept Relation = std::Predicate<R, T, U> || std::Predicate<R, U, T>;
根据我的理解,类型 T
和 U
之间的 relation 是对 (T, U)
上的二元谓词。因此, 评估两个 T
类型对象或两个 U
类型对象的关系是没有意义的。但是,给定的定义要求关系应该可以用参数 (T,T)
和 (U,U)
.
我的问题是:这个Relation
概念的定义(看似错误)背后的动机是什么?
cppreference 上给出的解释是
The concept Relation specifies that R defines a binary relation over the set of expressions whose type and value category are those encoded by either T or U.
(强调我的)
这对我来说听起来很奇怪:为什么一般的 Relation
概念被定义为 两种类型的两个支持参数 用于任意组合?
一种可能是这个概念用于指针和nullptr_t
的比较,以及迭代器和哨兵迭代器的比较。如果是这样,为什么这个概念被称为 Relation
,而不是更具体的东西,比如 InterComparable
?这只是用词不当吗?
用词不当。据我所知,它的存在是为了提供 StrictWeakOrder 之类的句法要求,而没有语义要求。
例如也可以考虑
template <class R, class T, class U>
concept Equivalence = std::Relation<R, T, U>;
//A relation r is an equivalence if
// - it is reflexive: for all x, r(x, x) is true;
// - it is symmetric: for all a and b, if r(a, b) is true then r(b, a) is true;
// - it is transitive: for all a, b and c, if r(a, b) and r(b, c) are both true then r(a, c) is true;