C++ Java like enum header 编译器错误

C++ Java like enum header compiler error

有人可以帮助解决以下编译器错误吗?我正在尝试在 C++ 中创建类似 Java 的枚举,但我在 Visual Studio 2015 中遇到编译器错误。

我正在使用这些 Java 像 Enum classes 作为结构的成员,我希望 sizeof(MessageType) 与值类型相同。我知道这将要求我从 class 中删除 mValueStr 成员,但是我将无法从字符串中查找值。任何关于我如何能够实现这一目标的建议将不胜感激。

Java 枚举中真正巧妙的事情之一是能够使用字符串名称或值查找枚举类型。为此,我向我的枚举 classes 添加了 2 个方法来从这些字符串或值中反向查找枚举。不幸的是,下面的方法有指示的波浪线

static MessageType valueOf(const int& rVal) {
    for (const auto& next : getValues()) {
        if (next == rVal) {
                 ~~
            return next;
        }
    }
    throw std::invalid_argument(
        "Illegal Argument: " + rVal);
}

编译器报错如下,我不太明白:

1>c:\main\dlmu\caclient\udpclient\cmc.h(123): error C2678: binary '==': no operator found which takes a left-hand operand of type 'const MessageType' (or there is no acceptable conversion)
1>  c:\main\dlmu\caclient\udpclient\cmc.h(123): note: could be 'built-in C++ operator==(MessageType::Value, int)'
1>  c:\main\dlmu\caclient\udpclient\cmc.h(123): note: while trying to match the argument list '(const MessageType, const int)'
1>  UDPClient.cpp
1>c:\main\dlmu\caclient\udpclient\cmc.h(123): error C2678: binary '==': no operator found which takes a left-hand operand of type 'const MessageType' (or there is no acceptable conversion)
1>  c:\program files (x86)\windows kits.1\include\shared\guiddef.h(192): note: could be 'bool operator ==(const GUID &,const GUID &)'
1>  c:\main\dlmu\caclient\udpclient\cmc.h(123): note: while trying to match the argument list '(const MessageType, const int)'

使此类功能正常工作的关键是将整数转换运算符用于值类型(在本例中为 e 位枚举值)

class MessageType {
public:
    enum class Value : uint8_t {
        undefined = 0, membersystem = 10
    };
    static const MessageType Undefined, MemberSystem;
    // integral operator cast for switch statements (cast to named enum)
    inline operator const Value() const {
        return mValue;
    }
    // strict weak ordering for set ops
    inline bool operator<(const MessageType& rhs) const {
        return mValue < rhs.mValue;
    }
    // serialized version of the enum
    inline std::string getStringVal() const {
        return mStringVal;
    }
    static const std::set<MessageType>& getValues() {
        static std::set<MessageType> gValues;
        if (gValues.empty()) {
            gValues.insert(Undefined);
            gValues.insert(MemberSystem);
        }
        return gValues;
    }
    static MessageType valueOf(const int& rVal) {
        for (const auto& next : getValues()) {
            if (next == rVal) {
                return next;
            }
        }
        throw std::invalid_argument(
            "Illegal Argument: " + rVal);
    }
    static MessageType valueOf(const std::string& rStringVal) {
        for (const auto& next : getValues()) {
            if (next.getStringVal() == rStringVal) {
                return next;
            }
        }
        throw std::invalid_argument(
            "Illegal Argument: " + rStringVal);
    }
private:
    MessageType(const Value& rValue, const std::string& rStringVal)
        : mValue(rValue)
        , mStringVal(rStringVal)
    {}

    Value mValue;
    std::string mStringVal;
};

真正奇怪的是,就在这个定义之前,我有一个类似的 class,它工作正常,没有波浪线,如下所示:

class DiscreteStatus {
public:
    enum Value : uint8_t {
        normaloperation = 0, nocomputeddata = 1, functionaltest = 2, failurewarning = 3
    };
    static const DiscreteStatus NormalOperation, NoComputedData, FunctionalTest, FailureWarning;
    // integral operator cast for switch statements (cast to named enum)
    inline operator const Value() const {
        return mValue;
    }
    // strict weak ordering for set ops
    inline bool operator<(const DiscreteStatus& rhs) const {
        return mValue < rhs.mValue;
    }
    // serialized version of the enum
    inline std::string getStringVal() const {
        return mStringVal;
    }
    static const std::set<DiscreteStatus>& getValues() {
        static std::set<DiscreteStatus> gValues;
        if (gValues.empty()) {
            gValues.insert(NormalOperation);
            gValues.insert(NoComputedData);
            gValues.insert(FunctionalTest);
            gValues.insert(FailureWarning);
        }
        return gValues;
    }
    static DiscreteStatus valueOf(const int& rVal) {
        for (const auto& next : getValues()) {
            if (next == rVal) {
                return next;
            }
        }
        throw std::invalid_argument(
            "Illegal Argument: " + rVal);
    }
    static DiscreteStatus valueOf(const std::string& rStringVal) {
        for (const auto& next : getValues()) {
            if (next.getStringVal() == rStringVal) {
                return next;
            }
        }
        throw std::invalid_argument(
            "Illegal Argument: " + rStringVal);
    }
private:
    DiscreteStatus(const Value& rValue, const std::string& rStringVal)
        : mValue(rValue)
        , mStringVal(rStringVal)
    {}

    Value mValue;
    std::string mStringVal;
};

实现文件(CPP)执行静态初始化如下:

const DiscreteStatus DiscreteStatus::NormalOperation(DiscreteStatus::Value::normaloperation, "NML");
const DiscreteStatus DiscreteStatus::NoComputedData(DiscreteStatus::Value::nocomputeddata, "NCD");
const DiscreteStatus DiscreteStatus::FunctionalTest(DiscreteStatus::Value::functionaltest, "FT");
const DiscreteStatus DiscreteStatus::FailureWarning(DiscreteStatus::Value::failurewarning, "FW");

const MessageType MessageType::Undefined(MessageType::Value::undefined, "Undefined");
const MessageType MessageType::MemberSystem(MessageType::Value::membersystem, "MemberSystem");

在这两种情况下,目的都是调用 type-conversion 运算符以执行到相应 Value 类型的转换。

在第一种情况下,您 Value 定义如下:

enum class Value : uint8_t

这不像 public 继承那样工作,但更类似于私有继承:它是一种 implemented-in-terms-of 关系 。客户端代码不得并且在您的情况下不能依赖 Value 在内部实现为 uint8_t.

这一事实

然而,在第二种情况下,它是这样的:

enum Value : uint8_t

这可以被描绘成类似 public 继承的东西,这意味着客户端代码可以依赖并依赖于 Value 实际上是 uint8_t 这一事实。因此类型转换有效。

现在,您可以通过将第一个定义更改为 enum Value : uint8_t 来 quick-fix 您的代码,但是有人想知道为什么您要提供带有整数参数的 valueOf 函数.它打破了抽象。


总的来说,我强烈反对这整个方法。这似乎过于复杂,当一个人试图用语言 B 编写语言 A 时,情况总是如此。

在这种特殊情况下,Java 枚举与 C++11 枚举 类 之间的任何相似之处都只是表面上的。最后,从 C++ 的角度来看,Java 枚举是 non-copyable 类,带有指向 garbage-collected 实例的静态常量指针,无法取消引用。你不想在 C++ 中模拟所有这些,你也不能,也不应该。