枚举类型的限定过多

Excessive qualifying of an enum type

本文关键字:类型 枚举      更新时间:2023-10-16

在此代码中

template<typename ValueT, typename ErrorT = bool>
class ValueOrError
{
private:
    enum class State
    {
        Undefined, OK, EndOfFile, InError
    };
public:
    ........
    template<typename U>
    static ValueOrError
    copyProblem(const ValueOrError<U, ErrorT>& other)
    {
        ValueOrError result;
        result.error_ = other.error_;
        result.state_ = other.state_;
        // result.state_ = static_cast<State>(other.state_);
        return result;
    }
private:
    template<typename U, typename E>
    friend class ValueOrError;
    State  state_;
    ValueT value_;
    ErrorT error_;
};

g++ 4.8.3在状态赋值时给出了一个错误。它抱怨不能分配类型为ValueError<U>::State的值为不同U和T的ValueError<T>::State类型值。静态强制类型转换绕过了这个问题。有更好的方法吗这样做呢?State类型显然不会随模板而变化参数。

这是一个方法:

struct State
{
    enum Enum{ Undefined, OK, EndOfFile, InError };
};
template< class ValueT, class ErrorT = bool>
class ValueOrError
    : private State
{
public: