Container for different functions?

后端 未结 1 1548
感动是毒
感动是毒 2021-02-14 14:29

I\'m trying to implement a container class for different functions where I can hold function pointers and use it to call those functions later. I\'ll try to discribe my problem

1条回答
  •  温柔的废话
    2021-02-14 15:10

    You may do something like:

    class BaseFunc {
    public:
        virtual ~BaseFunc() = default;
    
        virtual void Call(std::vector>& args_vec) const = 0;
    };
    
    template  class Function;
    
    template  class Function : public BaseFunc
    {
    public:
        Function(R (*f)(Args...)) : f(f) {}
        void Call(std::vector>& args_vec) const override
        {
            Call(args_vec, std::index_sequence_for());
        }
    private:
        template 
        void Call(
            std::vector>& args_vec,
            std::index_sequence) const
        {
            // Add additional check here if you want.
            f(boost::get(args_vec.at(Is))...);
        }
    
    private:
        R (*f)(Args...);
    };
    

    Live example

    0 讨论(0)
提交回复
热议问题