10

为了使我的枚举更加类型安全,我一直在使用宏生成的重载运算符来禁止将枚举与除相同类型枚举之外的任何内容进行比较:

#include <boost/static_assert.hpp>

#define MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, op) \
  template<typename T> \
  inline bool operator op(enumtype lhs, T rhs) \
  { \
    BOOST_STATIC_ASSERT(sizeof(T) == 0); \
    return false; \
  } \
  \
  template<> \
  inline bool operator op(enumtype lhs, enumtype rhs) \
  { \
    return static_cast<int>(lhs) op static_cast<int>(rhs); \
  }

#define MAKE_ENUM_TYPESAFE(enumtype) \
  MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, ==) \
  MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, !=) \
  MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, >) \
  MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, <) \
  MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, >=) \
  MAKE_ENUM_OPERATOR_TYPESAFE(enumtype, <=)

// Sample usage:
enum ColorType { NO_COLOR, RED, BLUE, GREEN };
MAKE_ENUM_TYPESAFE(ColorType)

这通常具有预期的效果;表单的color_variable == RED比较工作,而表单的比较color_variable == 1由于Boost.StaticAssert而产生编译时错误。(这是一个体面的方法吗?)

但是,我的编译器(CodeGear C++Builder)也在尝试使用这些重载的运算符来实现隐式bool转换。例如,if (color_variable) { ... }正在翻译if (operator!=(color_variable, 0)) { ... }并正在触发BOOST_STATIC_ASSERT和编译失败。

我相当确定这是我的编译器的不正确行为(例如,Comeau 和 GCC 不这样做)但想知道是否有任何语言律师可以确认。我自己尝试在 C++0x 标准草案中查找,但我只能找到第 4.12 节下的以下语句:

将零值、空指针值或空成员指针值转换为 false;任何其他值都将转换为 true。

没有关于如何检查“零值”的详细信息。

4

1 回答 1

4

你为什么不使用像下面这样的类?

template<class Enum>
class ClassEnum
{
public:
  explicit ClassEnum(Enum value) : value(value) {}
  inline bool operator ==(ClassEnum rhs) { return value == rhs.value; }
  inline bool operator !=(ClassEnum rhs) { return value != rhs.value; }
  inline bool operator <=(ClassEnum rhs) { return value <= rhs.value; }
  inline bool operator >=(ClassEnum rhs) { return value >= rhs.value; }
  inline bool operator  <(ClassEnum rhs) { return value < rhs.value; }
  inline bool operator  >(ClassEnum rhs) { return value > rhs.value; }

  // Other  operators...
private:
  Enum value;
}

enum ColorTypeEnum { NO_COLOR, RED, BLUE, GREEN };
typedef ClassEnum<ColorTypeEnum> ColorType;

没有隐式转换为 bool for ClassEnum<ColorTypeEnum>

于 2009-11-03T15:21:35.453 回答