7

我通过创建一个枚举类定义了一个元组及其索引:

/** parameter { key ; value1 ; value1 ; } */
using Parameter = std::tuple<unsigned, unsigned, unsigned>;
enum class ParameterKey : std::size_t {
    KEY = 0,
    VALUE1 = 1,
    VALUE2 = 2
};

现在我想从这个元组中得到一个值:

const auto& key = std::get<ParameterKey::KEY>(*parameterPointer);

我认为从intto的隐式转换是由语法std::size_t确保的:: std::size_t

enum class ParameterKey : std::size_t {
    ....
}

但我收到了这个错误

error: no matching function for call to ‘get<KEY>(std::tuple<unsigned int, unsigned int, unsigned int>&)’

这很好用,但是太啰嗦了:

const auto& key = std::get<static_cast<unsigned>(ParameterKey::KEY)>(*parameterPointer);
4

3 回答 3

5

这里没有隐式转换。从枚举

没有从作用域枚举器的值到整数类型的隐式转换,尽管可以使用 static_cast 来获取枚举器的数值。

所以,你必须使用static_cast.


有一些基于static_cast. 例如,可以使用std::underlying_type

template<typename T>
constexpr auto get_idx(T value)
{
    return static_cast<std::underlying_type_t<T>>(value);
}

进而:

const auto& key = std::get<get_idx(ParameterKey::KEY)>(*parameterPointer);
于 2017-08-11T10:24:51.210 回答
2

的整个目的enum class是不能隐式转换为int,因此没有隐式转换。

您可以创建自己的get版本:

template <ParameterKey key, typename Tuple>
decltype(auto) get(Tuple &&tuple) {
    return std::get<static_cast<std::underlying_type_t<ParameterKey>>(key)>(tuple);
}

然后:

const auto& key = get<ParameterKey::KEY>(*parameterPointer);
于 2017-08-11T10:26:33.380 回答
0

您可以通过创建接受此特定枚举作为参数的数组/向量的特化来进行隐式转换:

template <typename ElementType, typename EnumType>
class enumerated_array: array<ElementType, static_cast<size_t>(EnumType::size_)>
{
    using ParentType = array<ElementType, static_cast<size_t>(EnumType::size_)>;
public:
    ElementType& operator[](EnumType enumerator)
    {
        return ParentType::operator[](static_cast<size_t>(enumerator));
    }
    const ElementType& operator[](EnumType enumerator) const
    {
        return ParentType::operator[](static_cast<size_t>(enumerator));
    }
};

// --------------------------------
// and that's how you use it:

enum class PixelColor: size_t { Red, Green, Blue, size_ };
enumerated_array<uint8_t, PixelColor> pixel;
// Can't use any other enum class as an index
pixel[PixelColor::Green] = 255;

此外,虽然这不是这个问题的主题,但这种方法与枚举迭代器的协同作用非常好:

template <typename T>
class EnumRangeType
{
public:
    class Iterator
    {
    public:
        Iterator(size_t value):
            value_(value)
        { }

        T operator*() const
        {
            return static_cast<T>(value_);
        }

        void operator++()
        {
            ++value_;
        }

        bool operator!=(Iterator another) const
        {
            return value_ != another.value_;
        }

    private:
        size_t value_;
    };

    static Iterator begin()
    {
        return Iterator(0);
    }

    static Iterator end()
    {
        return Iterator(static_cast<size_t>(T::size_));
    }
};
template <typename T> constexpr EnumRangeType<T> enum_range;

// --------------------------------
// and that's how you use it:

void make_monochrome(enumerated_array<uint8_t, PixelColor>& pixel)
{
    unsigned int total_brightness = 0;
    for (auto color: enum_range<PixelColor>)
        total_brightness += pixel[color];

    uint8_t average_brightness = total_brightness/3;
    for (auto color: enum_range<PixelColor>)
        pixel[color] = average_brightness;
}    
于 2021-02-14T10:45:26.660 回答