两者都是the online C++ draft和 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>;
据我了解,relation T
和 U
类型之间是 (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;
关于c++ - C++20 中 "Relation"概念定义背后的动机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56274597/