I have defined a custom enum
for convenience over a particular class, and it is now getting in the way for getting it processed by more general processes.
How should I perform this typecast?
// A templated value holder:
template <typename T>
struct Holder {
T _value;
};
// A more general process..
template <typename T>
struct General {
Holder<T> *holder;
};
// ..over integral types:
struct IntGeneral : General<int> {};
// Here is something interesting: I can tell that this process will work on
// any enum type. But I've found no way to make this explicit.
// Convenience for a particular case
typedef enum {One, Two, Three} Enum;
typedef Holder<Enum> Particular;
int main() {
Particular* a( new Particular { One } );
IntGeneral ig { static_cast<Holder<int>*>(a) }; // compiler grumbles
return EXIT_SUCCESS;
}
This is what I get:
error: invalid static_cast from type ‘Particular* {aka Holder<Enum>*}’ to type ‘Holder<int>*’
Is there a way I can keep the convenient Enum
and get this code compiled?
EDIT: This turned out to be an XY problem. An answer to Y has been accepted here, and several discussed. X has been moved to another question.
Is it sufficient to replace:
IntGeneral ig { static_cast<Holder<int>*>(a) }; // compiler grumbles
With:
IntGeneral ig { new Holder<int>{a->_value} };
Since it is a struct, you can access the value. Personally I would use getters and setter, but as presented, that should do what you want.
Alternately
You could replace:
typedef Holder<Enum> Particular;
With:
typedef Holder<int> Particular;
Since the int
can hold the Enum