【发布时间】:2015-04-26 10:15:45
【问题描述】:
据我所知,目前不可能对 C++11 enum class 进行 typedef。我想知道在封装类之外引用变量enum 时是否有任何其他方法可以减少变量名称的长度。这是一个例子:
// I would like to do something along the lines of:
class SegmentActivityState;
using SegmentActivityType = SegmentActivityState::ActivityStateType;
// ...However, this results in the compile time error:
// 'SegmentActivityState' does not name a type.
// Enumeration class definition
class SegmentActivityState
{
public:
enum class ActivityStateType : Index
{
PreExecution = 0, /**< Pre-execution state. */
Execution = 1, /**< Execution state. */
PostExecution = 2 /**< Post-execution state. */
};
private:
ActivityStateType ActivityState;
/**< unique object identifier tag. */
public:
// ... Methods that work on the ActivityState
}
最重要的问题是我必须引用SegmentActivityType 之外的enum 的名称长度。例如,为了进行类型比较,我需要编写SegmentActivity.getState() == SegmentActivityState::ActivityStateType::PreExecution,这非常冗长。我不想做的两件事是:
-
typedefSegmentActivityState。 - 移动
enum classActivityStateTypeoutside of the classSegmentActivityState`定义。
【问题讨论】:
-
当您尝试创建类型别名时,符号
SegmentActivityState::ActivityStateType尚不存在。您可以简单地将它移到类定义之后。
标签: c++ c++11 enums typedef enum-class