小编Aar*_*sha的帖子

是否可以在 c++20 中使用比较运算符在枚举值之间建立小于大于顺序的关系

如果我们有一个这样的枚举类


enum class alpha{ a, b, c, d};

Run Code Online (Sandbox Code Playgroud)

是否可以实现一个运算符,在字母表中的字母之间建立排序关系,使得

enum class alpha{ a, b, c, d};

constexpr auto operator <=> (alpha lhs, alpha rhs)
{ 
//how do we achieve this? 
};

#include <gtest/gtest.h>

TEST(alphabet, allows_ordering_comparison) 
{
    EXPECT_TRUE(alpha::a < alpha::b);
} 

Run Code Online (Sandbox Code Playgroud)

小于比较将评估为真。我对此的平庸理解是 enum 是一种偏序。对代码中的错误表示歉意。考虑这个问题

c++ enums partial-ordering spaceship-operator c++20

0
推荐指数
1
解决办法
79
查看次数

标签 统计

c++ ×1

c++20 ×1

enums ×1

partial-ordering ×1

spaceship-operator ×1