[英]How to enforce a template to require a function predicate callable with a type or any of that type's derived types?
I have the following event dispatcher relationship:我有以下事件调度程序关系:
enum class Type {type1, type2, ...};
enum class Category {category1, category2, ...};
template <Category C, Type T>
class Event<C, T>{
public:
virtual std::string name() const = 0;
inline void consume(bool consume) { m_Consumed = consume; }
inline bool consumed() const { return m_Consumed; }
private:
bool m_Consumed = false;
};
template <Category C, Type T>
class Dispatcher {
Dispatcher(Event<C, T> & event) : m_Event(event) {}
template <class F> requires std::predicate<F, Event<C,T>>
void dispatch(const F & function) {
m_Event.consume(function(m_Event));
}
}
I'm unable to have the requires clause evaluate to true.我无法将 requires 子句评估为 true。 I think it's because the event parameter in my lambda function invocable is a derived type of
Event<C, T>
;我认为这是因为我的 lambda function invocable 中的事件参数是
Event<C, T>
的派生类型; it cannot match the predicate.它不能匹配谓词。 An example Event type is from the enum construction:
一个示例事件类型来自枚举构造:
class Tick : public Event<Instance, Tick> {};
Here is how I call it, for example:我是这样称呼它的,例如:
Instance::Tick event;
Dispatcher dispatcher(event);
dispatcher.dispatch([](const Instance::Tick & event) {
return true;
});
on the other hand, modifying the lambda with auto
compiles fine.另一方面,使用
auto
编译修改 lambda 可以正常编译。
dispatcher.dispatch([](const auto & event) {
return true;
});
Is there a way to expand the concept to include any of the derived types, if that's the case?如果是这样的话,有没有办法扩展这个概念以包括任何派生类型?
If there isn't, can the predicate only require that the function returns true and is invocable, without constraints on the parameters?如果没有,谓词是否可以只要求 function 返回 true 并且是可调用的,而不限制参数?
I have the following event dispatcher relationship:我有以下事件调度程序关系:
enum class Type {type1, type2, ...};
enum class Category {category1, category2, ...};
template <Category C, Type T>
class Event<C, T>{
public:
virtual std::string name() const = 0;
inline void consume(bool consume) { m_Consumed = consume; }
inline bool consumed() const { return m_Consumed; }
private:
bool m_Consumed = false;
};
template <Category C, Type T>
class Dispatcher {
Dispatcher(Event<C, T> & event) : m_Event(event) {}
template <class F> requires std::predicate<F, Event<C,T>>
void dispatch(const F & function) {
m_Event.consume(function(m_Event));
}
}
I'm unable to have the requires clause evaluate to true.我无法将 requires 子句评估为 true。 I think it's because the event parameter in my lambda function invocable is a derived type of
Event<C, T>
;我认为这是因为我的 lambda function invocable 中的事件参数是
Event<C, T>
的派生类型; it cannot match the predicate.它不能匹配谓词。 An example Event type is from the enum construction:
一个示例事件类型来自枚举构造:
class Tick : public Event<Instance, Tick> {};
Here is how I call it, for example:我是这样称呼它的,例如:
Instance::Tick event;
Dispatcher dispatcher(event);
dispatcher.dispatch([](const Instance::Tick & event) {
return true;
});
on the other hand, modifying the lambda with auto
compiles fine.另一方面,使用
auto
编译修改 lambda 可以正常编译。
dispatcher.dispatch([](const auto & event) {
return true;
});
Is there a way to expand the concept to include any of the derived types, if that's the case?如果是这样的话,有没有办法扩展这个概念以包括任何派生类型?
If there isn't, can the predicate only require that the function returns true and is invocable, without constraints on the parameters?如果没有,谓词是否可以只要求 function 返回 true 并且是可调用的,而不限制参数?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.