Suppose I have an EventHandle
class which is uniquely enumed by READ
, WRITE
, and SIGNAL
, and I am implementing a member template which should return different data type with respect to different enums.
enum class EventType {
READ,
WRITE,
SIGNAL
};
class EventHandle {
public:
template <EventType type, typename = enable_if_t<is_same<type, READ>::value>>
ReadEventHandle* cast () { return static_cast<ReadEventHandle>(this); }
template <EventType type, typename = enable_if_t<is_same<type, WRITE>::value>>
WriteEventHandle* cast () { return static_cast<WriteEventHandle>(this); }
template <EventType type, typename = enable_if_t<is_same<type, SIGNAL>::value>>
SignalEventHandle* cast () { return static_cast<SignalEventHandle>(this); }
};
And I have three derived classes from EventHandle
.
class ReadEventHandle : public EventHandle {...}
class WriteEventHandle : public EventHandle {...}
class SignalEventHandle : public EventHandle {...}
Apprently what I wrote doesn't compile. Is there any way to achieve this type cast overload on "compile" time (e.g., no switch on enum)?