- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
令我惊讶的是,我遇到了另一个障碍,例如 C++20 behaviour breaking existing code with equality operator? .
考虑一个简单的不区分大小写的键类型,用于例如 std::set
或 std::map
:
// Represents case insensitive keys
struct CiKey : std::string {
using std::string::string;
using std::string::operator=;
bool operator<(CiKey const& other) const {
return boost::ilexicographical_compare(*this, other);
}
};
简单测试:
using KeySet = std::set<CiKey>;
using Mapping = std::pair<CiKey, int>; // Same with std::tuple
using Mappings = std::set<Mapping>;
int main()
{
KeySet keys { "one", "two", "ONE", "three" };
Mappings mappings {
{ "one", 1 }, { "two", 2 }, { "ONE", 1 }, { "three", 3 }
};
assert(keys.size() == 3);
assert(mappings.size() == 3);
}
output.s: ./example.cpp:28: int main(): Assertion `mappings.size() ==3' failed.
operator<=>
在 C++20 模式下:
Compile Explorer
#if defined(__cpp_lib_three_way_comparison)
std::weak_ordering operator<=>(CiKey const& other) const {
if (boost::ilexicographical_compare(*this, other)) {
return std::weak_ordering::less;
} else if (boost::ilexicographical_compare(other, *this)) {
return std::weak_ordering::less;
}
return std::weak_ordering::equivalent;
}
#endif
问题
std::tuple::operator<
它应该有效:
3-6) Compares
lhs
andrhs
lexicographically byoperator<
, that is,compares the first elements, if they are equivalent, compares thesecond elements, if those are equivalent, compares the third elements,and so on. For non-empty tuples, (3) is equivalent toif (std::get<0>(lhs) < std::get<0>(rhs)) return true;
if (std::get<0>(rhs) < std::get<0>(lhs)) return false;
if (std::get<1>(lhs) < std::get<1>(rhs)) return true;
if (std::get<1>(rhs) < std::get<1>(lhs)) return false;
...
return std::get<N - 1>(lhs) < std::get<N - 1>(rhs);
Compares
lhs
andrhs
lexicographically by synthesized three-waycomparison (see below), that is, compares the first elements, if theyare equivalent, compares the second elements, if those are equivalent,compares the third elements, and so on
The <, <=, >, >=, and != operators are synthesized from
operator<=>
andoperator==
respectively. (since C++20)
operator<=>
也不是 operator==
,operator<
另外会很好,应该在评估像 a < b
这样的简单表达式时使用。 .tuple
中的用户定义类型/pair
不能很好地扩展。 tuple
还有其他类型吗?/pair
这可能会表现出类似的变化? 最佳答案
基本问题来自于您的类型不连贯并且标准库直到 C++20 才调用您的事实。也就是说,你的类型总是有点坏,但事情的定义足够狭隘,你可以侥幸逃脱。
您的类型已损坏,因为它的比较运算符没有意义。它宣称它是完全可比较的,并定义了所有可用的比较运算符。发生这种情况是因为您公开继承了 std::string
,因此您的类型通过隐式转换为基类来继承这些运算符。但是这种比较表的行为是不正确的,因为您只替换了其中一个比较与其他比较不起作用。
而且由于行为不一致,一旦 C++ 真正关心你的一致性,可能发生的事情就可以解决了。
然而,更大的问题是与标准处理 operator<=>
的方式不一致。 .
C++ 语言的设计目的是在使用综合运算符之前优先考虑显式定义的比较运算符。所以你的类型继承自 std::string
将使用您的 operator<
如果你直接比较它们。
然而,C++ 库有时会尝试变得聪明。
某些类型尝试转发给定类型提供的运算符,例如 optional<T>
.它的设计行为与 T
相同。在它的可比性上,它在这方面取得了成功。
但是,pair
和 tuple
试着聪明一点。在 C++17 中,这些类型实际上从未转发比较行为;相反,它基于现有的 operator<
综合比较行为和 operator==
类型的定义。
因此,他们的 C++20 版本延续了综合比较的优良传统也就不足为奇了。当然,由于该语言进入了该游戏,C++20 版本决定最好遵循他们的规则。
除了......它不能完全跟随他们。无法检测 <
是否存在比较是综合的或用户提供的。因此,无法以其中一种类型实现语言行为。但是,您可以检测到存在三向比较行为。
所以他们做了一个假设:如果你的类型是三向可比的,那么你的类型依赖于综合运算符(如果不是,它使用旧方法的改进形式)。这是正确的假设;毕竟,自从 <=>
是一项新功能,旧类型可能无法获得。
当然,除非旧类型继承自获得三向可比性的新类型。并且类型也无法检测到这一点。它要么是三向可比的,要么不是。
现在幸运的是,pair
的综合三路比较运算符和 tuple
如果您的类型不提供三向比较功能,则完全能够模仿 C++17 行为。因此,您可以通过删除 operator<=>
显式地取消继承 C++20 中的三向比较运算符来恢复旧行为。重载。
或者,您可以使用私有(private)继承并简单地公开 using
您想要的特定 API。
Is the behavior change in c++20 correct/on purpose?
std::string
等类型公开继承在道德上一直有些可疑。与其说是因为切片/析构函数问题,不如说是因为它有点作弊。直接继承此类类型会导致 API 发生您未预料到且可能不适合您的类型的更改。
pair
的新比较版和
tuple
正在做他们的工作,并在 C++ 允许的范围内做到最好。只是你的类型继承了它不想要的东西。如果您从
std::string
私下继承并且只有
using
-暴露了您想要的功能,您的类型可能会很好。
Should there be a diagnostic?
Can we use other tools to spot silent breakage like this?
关于c++ - 使用 C++20 三向比较进行更静默的行为变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66497269/
嘿伙计们。 实现背景变化(基本上是幻灯片放映)和过渡效果的常见方法有哪些。我想每隔一段时间改变complte文档背景。 我是一名 ASP.net 开发人员,并且希望大部分内容都可以在 ASP 中实现。
也许,指针已经在修改过程中指向 auto_ptr 的常规指针指向 unique_ptr 和 shared_ptr 我只是想知道已经开发出来的新型指针是否完全覆盖了旧版本(或者您可能认为存在内存泄漏问题
我使用 Android Studio 构建 Android 应用。 我的问题是:当 fragment 改变时,应用程序崩溃。 控制台输出[控制台] 01-06 18:35:21.952 27756-
****澄清**我做了这个 [Fiddle] ( http://jsfiddle.net/sggPv/10/ ) 来帮助澄清情况。 该脚本起初适用于两个表格,但随后当您点击 slider 并将新表格加
我有图标,单击它会将新的 div(列)添加到 div 容器。问题是,当新的 div(列)出现时,按钮不会向右移动。是否可以以某种方式仅在 div 内添加 position:fixed? 这是我的几个屏
我是 Java 新手,继承了现有的 Android 应用程序。原始开发人员选择使用常量接口(interface)。 我的问题是我需要更改其中一些常量来编译生产应用程序与开发应用程序。如果我手动修改一些
在 Apple developer Document 中,我在 UIColor 中发现了一些新东西。 If your app was linked on or after iOS 10 and whe
我没有经常使用 ShareKit,但我只想拥有三个共享选项:Facebook、Twitter 和电子邮件。 ShareKit 提供了更多选项,包括更多按钮。但是,我不想要“更多”选项,只想要三个。 在
我正在构建一个 JS 库,其中一个用例要求我在 DOM 更改时触发一个事件,特别是如果它是一个单页应用程序,例如:github search bar 经过一番研究,我遇到了MutationObserv
我已经设法编写了一个代码来检测任何工作表中特定单元格的值变化,但我一直在努力构建检测和跟踪范围(值)变化的东西。 例如,如果用户决定复制和粘贴某个范围的数据(假设超过 1 个单元格),它不会被宏捕获。
使用 ffmpeg ,我们可以对音频电平进行多少控制?例如,我想在程序的时间轴上映射一个“M”形: t0 - t1 : fade in from 0 to 1 t1 - t2 : play at fu
使用 jQuery 1.7.1,我尝试为下拉列表上的更改事件创建一个事件处理程序。下拉列表会动态添加到 DOM 中。似乎在大多数浏览器上都能很好地工作,但是哦,奇怪的 IE8 想要变得困难。有解决方法
我想制作一个具有可选边框大小的自定义控件。请参阅下面的代码。边框绘制在非客户区,其宽度可以是 0、1 或 2 像素。我已经在 WM_NCPAINT 中成功完成了边框绘制。问题是,在更改控制边框大小的属
我知道这个问题之前已经被问过,而且我实际上已经找到了一些我已经实现的解决方案。不幸的是,我没能得到我想要的。 我以前没有做过AngularJS,我想做的是: 检测网址何时更改 根据网址更改的内容进行一
我有一个 auto-carousel 指令,它循环访问链接元素的子元素。 但是,子级尚未加载到 DOM 中,因为它们的 ng-if 表达式尚未解析。 如何确保父指令知道其 DOM 树已发生更改?
我有一个流程可以通过内容提供商从数据库中获取数据。 fun getDataFlow(): Flow { return flow { emit(Result.Loading)
我有一些有效的代码,但有时它只是“跳转”到其他文本而不考虑间隔。 该代码基本上按时间间隔更改标题的文本。 var text = ["text1", "text2", "text3","text4","
我正在尝试将 onCLick 监听器添加到我的 PreferenceScreen 上的开关,但它不起作用。我尝试了 Java 教程中的代码并将其转换为 Kotlin,但由于某种原因它无法正常工作。 这
我们目前正在尝试升级我们的程序使用的 ffmpeg 版本。跳跃很大,因为我们目前使用的是 ffmpeg 0.8,最新版本是 1.2。 在这些测试中,我使用的是(让我说)我发现的令人惊叹的软件包 her
我有一个流程可以通过内容提供商从数据库中获取数据。 fun getDataFlow(): Flow { return flow { emit(Result.Loading)
我是一名优秀的程序员,十分优秀!