c++ - C++20 中 "Relation"概念定义背后的动机

标签 c++ c++-standard-library c++20 c++-concepts

两者都是the online C++ draftcppreference像这样定义标准的 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 TU 类型之间是 (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/

相关文章:

c++ - 如何在 Windows 7 上编译 ASL(基于 boost 的 Adob​​e C++ gui 库)?

c++ - 将模板与类模板推导占位符参数匹配

c++ - 自移动 std::string 是否可移植?

c++ - 从专用模板方法调用非专用模板方法

c++ - 随机数据测试

c++ - Scoped Reference Idiom 的最佳实践?

c++ - std::vector 是否可以简单复制,为什么?

c++ - 什么是 std::false_type 或 std::true_type?

c++ - 如何正确初始化 std::array 的 vector ?

c++ - 按值排序的 std::find 和 std::any_of