Creating a composite type from two enum classes, ready for STL map

后端 未结 6 1689
星月不相逢
星月不相逢 2021-02-14 05:26

I would like to create a composite type out of two enum classes.

enum class Color {RED, GREEN, BLUE};
enum class Shape {SQUARE, CIRCLE, TRIANGLE};

         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-14 05:50

    As commented and as already stated by others, give precedence to either Shape or Color in the operator< and only compare the other if the first is equal. An alternative implementation for operator< using std::tie:

    #include 
    friend bool operator<(const Object& lhs, const Object& rhs)
    {
        return std::tie(lhs.color, lhs.shape) < std::tie(rhs.color, rhs.shape);
    }
    

提交回复
热议问题