简体   繁体   English

如何强制模板要求 function 谓词可使用类型或任何该类型的派生类型调用?

[英]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.

相关问题 如何在模板类型中强制执行静态成员初始化? 或如何获取从模板类型派生的所有类的计数? - How to enforce static member initialization in template types? or How to get the count of all classes, that were derived from template type? std :: function的copy-constructor是否要求模板类型的参数类型为完整类型? - Does std::function's copy-constructor require the template type's argument types to be complete types? C ++:如何要求一个模板类型派生自另一个模板类型 - C++: How to require that one template type is derived from the other 模板函数,它接受类型的对象和所有派生类型的对象 - template function which takes objects of type and objects of all derived types 选择具有派生类型的基类型的模板特化 - Choosing template specialization of a base type with derived types 强制模板函数参数成为特定类型的迭代器 - Enforce template function parameters to be iterators on a specific type 用作模板参数,对返回类型强制约束? - Function as template parameter, enforce constraint on return type? 错误C2893无法专门化函数模板&#39;unknown-type std :: invoke(_Callable &amp;&amp;,_ Types &amp;&amp; ...)&#39; - Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&…)' 如何对模板类型强制执行常量? - How to enforce constness over template type? 要求功能模板的返回类型是模板的专用化 - Require return type of function template to be specialization of a template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM