【发布时间】:2016-05-10 19:46:27
【问题描述】:
假设我有一个由READ、WRITE 和SIGNAL 唯一枚举的EventHandle 类,并且我正在实现一个成员模板,它应该针对不同的枚举返回不同的数据类型。
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); }
};
我有三个来自EventHandle 的派生类。
class ReadEventHandle : public EventHandle {...}
class WriteEventHandle : public EventHandle {...}
class SignalEventHandle : public EventHandle {...}
显然我写的内容无法编译。有没有办法在“编译”时实现这种类型转换重载(例如,没有打开枚举)?
【问题讨论】:
-
你为什么需要这个
EventHandler和这个枚举类型。考虑到您有 3 个课程,这似乎完全没用 - 为什么您需要“投射”..... -
请发布您遇到的编译器错误。