C++ enum class std::size_t 隐式转换
C++ enum class std::size_t implicit conversion
我通过创建枚举定义了一个元组及其索引 class:
/** 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);
我认为从 int
到 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);
这里没有隐式转换。来自 enum:
There are no implicit conversions from the values of a scoped
enumerator to integral types, although static_cast may be used to
obtain the numeric value of the enumerator.
因此,您必须使用 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);
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);
您可以通过创建接受此特定枚举作为参数的 array/vector 的特化来隐式转换:
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;
}
我通过创建枚举定义了一个元组及其索引 class:
/** 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);
我认为从 int
到 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);
这里没有隐式转换。来自 enum:
There are no implicit conversions from the values of a scoped enumerator to integral types, although static_cast may be used to obtain the numeric value of the enumerator.
因此,您必须使用 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);
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);
您可以通过创建接受此特定枚举作为参数的 array/vector 的特化来隐式转换:
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;
}