【发布时间】:2021-04-15 08:03:03
【问题描述】:
我在调试 this question 时遇到了这个问题。
我一直把它删减到只使用Boost Operators:
-
#include <boost/operators.hpp> struct F : boost::totally_ordered1<F, boost::totally_ordered2<F, int>> { /*implicit*/ F(int t_) : t(t_) {} bool operator==(F const& o) const { return t == o.t; } bool operator< (F const& o) const { return t < o.t; } private: int t; }; int main() { #pragma GCC diagnostic ignored "-Wunused" F { 42 } == F{ 42 }; // OKAY 42 == F{42}; // C++17 OK, C++20 infinite recursion F { 42 } == 42; // C++17 OK, C++20 infinite recursion }这个程序可以在 GCC 和 Clang 中使用 C++17(启用 ubsan/asan)编译和运行。
-
当你将隐式构造函数改为
explicit时,问题行明显no longer compile on C++17
令人惊讶的是,两个版本在 C++20 上编译(v1 和 v2),但它们会导致 无限递归(崩溃或紧密循环,具体取决于优化级别)在不能在 C++17 上编译的两行上。
显然,这种通过升级到 C++20 潜入的无声错误令人担忧。
问题:
- 这是否符合 c++20 行为(我希望如此)
- 究竟是什么干扰?我怀疑这可能是由于 c++20 新的“宇宙飞船操作员”支持,但不明白它如何改变了这段代码的行为。
【问题讨论】:
-
@ShafikYaghmour 你们真的很快。世界不值得这种程度的支持。谢谢
-
我不认为这是一个骗局,但它肯定是相关的stackoverflow.com/questions/64130311
-
@cigien 感谢。答案中的解释非常好,有助于形成更完整的理解。
标签: c++ c++17 c++20 spaceship-operator