gpt4 book ai didi

c++ - C++20概念中 "equal"的概念

转载 作者:太空狗 更新时间:2023-10-29 21:31:49 26 4
gpt4 key购买 nike

在阅读一些概念定义时,我多次发现 equal 的使用,例如 Swappable:

Let t1 and t2 be equality-preserving expressions that denote distinct equal objects of type T,

equal 是否在标准的某处定义?我猜这意味着两个对象的语义,或者它们引用的值(赋予它们表示的域值的人类语义)是相同的,即使对象不可比较(没有 operator==重载),或类似的抽象事物(例如,两个对象 ab 是相等的,如果 a == b 会产生 true 假设它是一个有效的表达式——例如,因为 operator== 没有定义,因为它不是必需的)。

最佳答案

由于模板将处理用户提供的语义,相等的概念主要由程序定义。例如,如果我使用不区分大小写的字符串,我可以认为字符串 FoOfOo 相等,而 FoO bAr 不相等。我提供的操作必须反射(reflect)这种语义。

相等性不是基于operator==定义的;相反,operator== 是(在某种意义上)基于相等性定义的。 [concept.equalitycomparable]/equality_comparable :

template<class T>
concept equality_comparable = weakly-equality-comparable-with<T, T>;

Let a and b be objects of type T. T models equality_­comparable only if bool(a == b) is true when a is equal to b ([concepts.equality]), and false otherwise.

[ Note: The requirement that the expression a == b is equality-preserving implies that == is transitive and symmetric. — end note ]

关于c++ - C++20概念中 "equal"的概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57129633/

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