variadic template class to make a deferred call to a variadic template function

前端 未结 2 628
无人共我
无人共我 2021-01-24 06:07

I can create a template class that stores some values in a property and let me later call a method that call a function with this arg. Like this :

template <         


        
2条回答
  •  梦毁少年i
    2021-01-24 06:38

    You may use something like the following:

    template  class B
    {
    public:
        std::tuple t;
        B(Ts... args)
            : t(args...)
        {
        }
        void m() { call_f(std::index_sequence_for()); }
    
    private:
        template 
        void call_f(std::index_sequence)
        {
            f(std::get(t)...);
        }
    };
    

    Note that std::index_sequence_for (std::make_index_sequence) and std::index_sequence are C++14 but may be written in C++11.

    Live example.

提交回复
热议问题