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

匆匆过客 提交于 2019-12-02 11:31:23

问题


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 <typename U> void g(U u) { cout << u << endl; }
template <typename U> class C {
public:
        U u;
        C(U u) { this->u = u; }
        void m() { g(u); }
};

int main() {
        C<double> c(5.5);
        c.m();
}

But how to make the same with variadic templates ? I would like to write something like :

template <typename ... T> void f(T... a) { cout << "generik" << endl; }

template <typename ... T> class B {
    public:
        T... arg;
        B(T... arg) {
             this->arg = arg;
        }
        void m() { f(arg); }
};

int main() {
    B<int,double> b(1,1.1);
    b.m();
}

I know that it will not work because we cannot declare a member of unpacked parameter type.

I can do a pattern matching for some parameters of the list and then call the function if a given number of parameters is given, but I want to do it in a generic way. Is there an elegant way to do it ?


回答1:


http://ideone.com/OPl7Rz

#include <iostream>
#include <functional>

using namespace std;

template<typename... T>
void f(T... a)
{
    std::initializer_list<int> {(std::cout<<a<<" ", 0)...};
}

template<typename... T>
class Defer
{
    private:
        std::function<void()> func;

    public:
        Defer(T... a) : func(std::bind(f<T...>, a...)) {}
        void call() {func();}
};



int main()
{
    Defer<int, float, int, const char*> d(1, 1.1, 2, "Hey");
    d.call();
    return 0;
}



回答2:


You may use something like the following:

template <typename... Ts> class B
{
public:
    std::tuple<Ts...> t;
    B(Ts... args)
        : t(args...)
    {
    }
    void m() { call_f(std::index_sequence_for<Ts>()); }

private:
    template <std::size_t ... Is>
    void call_f(std::index_sequence<Is...>)
    {
        f(std::get<Is>(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.



来源:https://stackoverflow.com/questions/27280629/variadic-template-class-to-make-a-deferred-call-to-a-variadic-template-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!