这是创建枚举扩展行为的说明。我已经这样实现了:
继承num.h:
template <typename BaseEnumT, typename EnumT>
class InheritEnum{
public:
InheritEnum() {}
InheritEnum(EnumT e): enumt_(e){}
InheritEnum(BaseEnumT e): baseEnum_(e){}
explicit InheritEnum(int val): enumt_(static_cast<EnumT>(val)){}
operator EnumT() const { return enumt_; }
private:
EnumT enumt_;
BaseEnumT baseEnum_;
};
然后在main.cpp:
enum class Fruit { apple, orange };
enum class Drink { water, milk };
typedef InheritEnum<Fruit, Drink> Eatable;
void GetIt(Eatable eatable) {}
int main(){
GetIt(Fruit::apple);
}
我可以扩展行为以继承另一种这样的类型
继承编号.h
template <typename BaseEnumT, typename EnumT, typename EnumV>
class InheritEnum{
public:
InheritEnum() {}
InheritEnum(EnumT e): enumt_(e){}
InheritEnum(EnumV e): enumv_(e){}
InheritEnum(BaseEnumT e): baseEnum_(e){}
explicit InheritEnum(int val): enumt_(static_cast<EnumT>(val)), enumv_(static_cast<EnumV>(val)){}
operator EnumT() const { return enumt_; }
operator EnumV() const { return enumv_; }
private:
EnumV enumv_;
EnumT enumt_;
BaseEnumT baseEnum_;
};
然后在main.cpp:
enum class Fruit { apple, orange };
enum class Drink { water, milk };
enum class Food { pasta, fish };
typedef InheritEnum<Fruit, Drink, Food> Eatable;
void GetIt(Eatable eatable) {}
int main(){
GetIt(Food::fish);
}
问题是如何在这里使用参数包,而不是手动添加新类型:
template <typename BaseEnumT, typename... EnumG>
class InheritEnum{
// how to modify the original code
}