与C++ 11枚举的位操作

发布于 2025-01-17 22:10:37 字数 997 浏览 4 评论 0原文

我有这个C ++ 11 ,其中包含各种enum s。这些enum s包含 bitmasks ,最终使用 bitwise Operators 将所有最终都合并为8位值。

看起来像这样:

class SX1509
{
public:

  enum class DriverMode
  {
    LINEAR = 0b00000000,
    LOGARITHMIC = 0b10000000
  };

  enum class ClockSpeed
  {
    ULTRA_FAST = 0b00010000,
    EXTRA_FAST = 0b00100000,
    FAST = 0b00110000,
    MEDIUM = 0b01000000,
    SLOW = 0b01010000,
    EXTRA_SLOW = 0b01100000,
    ULTRA_SLOW = 0b01110000,
  };

  SX1509()
  {
    this->config(DriverMode::LOGARITHMIC, ClockSpeed::EXTRA_FAST)
  };

  void config(DriverMode driverMode, ClockSpeed clockSpeed) {
    uint8_t config = driverMode | clockSpeed;
    i2c->write(config);
  }

}

但是,在将所有enum定义更改为enum class之后,编译器吐出了此错误,我不明白,这

error: no match for 'operator|' (operand types are 'SX1509::DriverMode' and 'SX1509::ClockSpeed')

是在这里发生了什么?

I have this c++11 class which contains a variety of enums. These enums contain bitmasks which eventual all get combined together into an 8-bit values using bitwise operators.

It looks like this:

class SX1509
{
public:

  enum class DriverMode
  {
    LINEAR = 0b00000000,
    LOGARITHMIC = 0b10000000
  };

  enum class ClockSpeed
  {
    ULTRA_FAST = 0b00010000,
    EXTRA_FAST = 0b00100000,
    FAST = 0b00110000,
    MEDIUM = 0b01000000,
    SLOW = 0b01010000,
    EXTRA_SLOW = 0b01100000,
    ULTRA_SLOW = 0b01110000,
  };

  SX1509()
  {
    this->config(DriverMode::LOGARITHMIC, ClockSpeed::EXTRA_FAST)
  };

  void config(DriverMode driverMode, ClockSpeed clockSpeed) {
    uint8_t config = driverMode | clockSpeed;
    i2c->write(config);
  }

}

However, after changing all my enum definitions to enum class the compiler spit out this error which I do not understand ????

error: no match for 'operator|' (operand types are 'SX1509::DriverMode' and 'SX1509::ClockSpeed')

What is going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风向决定发型 2025-01-24 22:10:38

枚举类枚举不会被隐式转换为整数,这与常规枚举不同。

我至少要做两件事。首先,明确指示enum类的基本类型,例如:

  enum class DriverMode : uint8_t
  {
    LINEAR = 0b00000000,
    LOGARITHMIC = 0b10000000
  };

虽然这无助于明确的转换,但由于从签名到未签名的转换,它将有助于扭转。

其次,明确转换枚举值,例如,

uint8_t config = static_cast<uint8_t>(driverMode) | static_cast<uint8_t>(clockSpeed);

您可能需要使用typedef别名uint8_t,例如,

typedef uint8_t MyEnumUnderlyingType;

而不是使用RAW uint> uint8_t

enum class enumerations do not get implicitly converted to integers, unlike regular enumerations.

I would do at least two things. First, explicitly indicate the underlying type of the enum class, e.g.:

  enum class DriverMode : uint8_t
  {
    LINEAR = 0b00000000,
    LOGARITHMIC = 0b10000000
  };

While this won't help with explicit conversion, it will help with wonkiness due to conversion from signed to unsigned.

Second, explicitly convert the enumeration values, e.g.,

uint8_t config = static_cast<uint8_t>(driverMode) | static_cast<uint8_t>(clockSpeed);

You may want to use a typedef alias for uint8_t, e.g.,

typedef uint8_t MyEnumUnderlyingType;

instead of using the raw uint8_t.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文