- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:这与飞船无关。只是飞船的使用混淆了我代码中的真正问题(详见答案)。
我对这个 program 的输出感到惊讶:
(如果您喜欢谜题,请随时打开 Godbolt 链接并尝试自己找出原因)
#include <cstdint>
#include <cassert>
#include <compare>
#include <cmath>
#include <iostream>
#include <limits>
template<typename T>
struct TotallyOrdered
{
T val;
constexpr TotallyOrdered(T val) :
val(val) {}
constexpr operator T() const { return val; }
constexpr std::strong_ordering operator<=>(TotallyOrdered const& other) const
{
if (std::isnan(val) && std::isnan(other.val))
{
return std::strong_ordering::equal;
}
if (std::isnan(val))
{
return std::strong_ordering::less;
}
if (std::isnan(other.val))
{
return std::strong_ordering::greater;
}
if (val < other.val)
{
return std::strong_ordering::less;
}
else if (val == other.val)
{
return std::strong_ordering::equal;
}
else
{
assert(val > other.val);
return std::strong_ordering::greater;
}
}
};
int main()
{
const auto qNan = std::numeric_limits<float>::quiet_NaN();
std::cout << std::boolalpha;
std::cout << ((TotallyOrdered{qNan} <=> TotallyOrdered{1234.567}) == std::strong_ordering::less) << std::endl;
std::cout << ((TotallyOrdered{qNan} <=> TotallyOrdered{1234.567}) == std::strong_ordering::equal) << std::endl;
std::cout << ((TotallyOrdered{qNan} <=> TotallyOrdered{1234.567}) == std::strong_ordering::equivalent) << std::endl;
std::cout << ((TotallyOrdered{qNan} <=> TotallyOrdered{1234.567}) == std::strong_ordering::greater) << std::endl;
}
输出:
false
false
false
false
TotallyOrdered<float>
和
TotallyOrdered<double>
(在
f
之后添加
1234.567
给出预期输出)。
strong_order
,但在进行混合比较时,我似乎得到了偏序。 <=>
. std::strong_ordering
结果),从而阻止给出 std::partial_ordering
的比较? 最佳答案
这是允许的,因为您的转换运算符为 T
不明确。这允许比较的双方进行用户定义的转换到各自的 T
.所以你最终得到一个 float
和一个 double
.然后这些都可以转换为 double
并且可以进行比较。但该比较返回 std::partial_ordering
,不是 std::strong_ordering
.
请注意 std::strong_ordering
可以与 bool 进行比较,这就是您的代码首先编译的原因。尽管 cppreference.com 确实注意到:
The behavior of a program that attempts to compare a strong_ordering with anything other than the integer literal 0 is undefined.
关于c++ - 为什么宇宙飞船允许混合比较(不同的模板实例)与无意义的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66384530/
实际上,我正在编写一个小型太空射击游戏(2.5D,俯 View )。玩家可以沿着 XZ 轴移动并通过右侧的迷你摇杆(游戏 handle )旋转宇宙飞船或查看光标位置(键盘 + 鼠标)。 因此,运动和旋
这里是初级程序员,只是想了解Ruby背后的过程sort使用飞船操作符时的方法 .希望有人能帮忙。 在以下内容中: array = [1, 2, 3] array.sort { |a, b| a b
什么是 Ruby (宇宙飞船)运算符(operator)?该运算符是否由任何其他语言实现? 最佳答案 spaceship operator将返回 1、0 或 −1,具体取决于左侧参数相对于右侧参数的
我想在 window 的中央画一艘宇宙飞船,因为它在太空中提供动力。为了滚动世界窗口,我计算了飞船的新位置,并使用 glOrtho 将一个 4000x3000 的窗口居中。我的测试是按下前进按钮并向上
我是一名优秀的程序员,十分优秀!