数组索引(转换为整数)与范围枚举 [英] array indexing (converting to integer) with scoped enumeration

查看:123
本文介绍了数组索引(转换为整数)与范围枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 11范围内的统计员(枚举类语法)没有转换为整数,所以不能直接作为数组索引。

C++11 scoped enumerators (enum class syntax) do not convert to integers so they cannot be used directly as array indexes.

什么是在这种方式使用的时候他们得到作用域的利益的最佳方式?

What's the best way to get the benefit of scoping when using them in this fashion?

我提供一对夫妇的答案,但请添加更多的想法!

I've provided a couple answers, but please add more ideas!

推荐答案

这是我目前最喜欢的。超载一元运营商+ 符++ 来显式转换为整数类型和增量枚举类型中,分别为。

Solution 1: Operator overloading.

This is my current favorite. Overload unary operator+ and operator++ to explicitly convert to integral type and increment within the enumerated type, respectively.

使用 enumeration_traits 模板,重载可以被激活,而不是复制的样板code。但样板只是一对夫妇的俏皮话。

Using an enumeration_traits template, overloads can be activated rather than copying boilerplate code. But the boilerplate is just a couple one-liners.

库code(模板,请参见下面的非模板二选一):

Library code (templates, see below for non-template alternative):

template< typename e >
struct enumeration_traits;

struct enumeration_trait_indexing {
    static constexpr bool does_index = true;
};

template< typename e >
constexpr
typename std::enable_if< enumeration_traits< e >::does_index,
    typename std::underlying_type< e >::type >::type
operator + ( e val )
    { return static_cast< typename std::underlying_type< e >::type >( val ); }

template< typename e >
typename std::enable_if< enumeration_traits< e >::does_index,
    e & >::type
operator ++ ( e &val )
    { return val = static_cast< e >( + val + 1 ); }

用户code:

enum class ducks { huey, dewey, louie, count };
template<> struct enumeration_traits< ducks >
    : enumeration_trait_indexing {};

double duck_height[ + ducks::count ];

样板code(如果不使用库,如下枚举定义):

int operator + ( ducks val )
    { return static_cast< int >( val ); }

ducks &operator ++ ( ducks &val )
    { return val = static_cast< ducks >( + val + 1 ); }

解决方案2:手动作用域

作用域枚举语法也适用于无作用域(非枚举类)的枚举,这也隐式转换为 INT 。隐藏枚举类或命名空间内,并与的typedef 导入,或使用使得伪范围。

Solution 2: Manual scoping.

Scoped enumerator syntax also works on unscoped (non enum class) enumerations, which do implicitly convert to int. Hiding the enumeration inside a class or namespace and importing it with typedef or using makes it pseudo-scoped.

但如果多个枚举进入同一个命名空间,枚举器的名称可能发生冲突,所以你还不如用一个类(或多个命名空间)。

But if multiple enumerations go into the same namespace, the enumerator names may collide, so you might as well use a class (or many namespaces).

struct ducks_enum {
    enum ducks { huey, dewey, louie, count };
};
typedef ducks_enum::ducks ducks;

double duck_height[ ducks::count ]; // C++11
double duck_weight[ ducks_enum::count ]; // C++03

这有一定的好处。 它与C ++ 03 ,但只能用语法 ducks_enum ::计数。统计员是无范围的结构体里面,它可以被用来作为,使频繁使用统计员的任何类的魅力。

This has some benefits. It works with C++03, but only with syntax ducks_enum::count. The enumerators are unscoped inside the struct, and it can be used as a base for any class that makes frequent use of the enumerators.

这篇关于数组索引(转换为整数)与范围枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆