为什么 C++20 中的空结构没有隐式飞船运算符?

问题描述

动机:有时我使用 std::variant 来实现“花哨的”枚举,其中一些枚举状态可以携带状态。

现在,如果我想对我的变体使用 <=>,它需要我的空结构已经定义了 。 这对我来说似乎有点奇怪,因为如果类型具有 0 位状态,则该类型的所有实例都相同。

完整的example

#include <compare>
#include <iostream>
#include <variant>

struct Off{
    // without this the code does not compile
    auto operator<=>(const Off& other) const = default;
};

struct Running{
    int rpm=1000;
    auto operator<=>(const Running& other) const = default;
};

using EngineState = std::variant<Off,Running>;

int main()
{
    EngineState es1,es2;
    es1<=>es2;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)