mercredi 2 décembre 2015

how unpack tuple for wrapper function parameter in c++11?

//function wapper
template<class F,typename...Args>
class FuncWrapper
{
private:
    //F return type
    using return_type = typename std::result_of<F(Args...)>::type;

    template<int ...S> struct IndexSeq {};
    template<int N, int ...S> struct makeSeq : makeSeq<N - 1, N - 1, S...> {};
    template<int ...S> struct makeSeq<0, S...> : IndexSeq<S...> {};


public:
    FuncWrapper(F &&func,Args&&...args):
        f(std::move(std::function<return_type(Args...)>(std::forward<F>(func)))),
        para(std::make_tuple<Args...>(std::forward<Args>(args)...))
    {}
    ~FuncWrapper() = default;

    template <int... INDEX>
    return_type delay_action(makeSeq<INDEX...>)
    {
        return f(std::get<INDEX>(para)...);
    }

    //
    return_type operator()()
    {
        return  delay_action(makeSeq<sizeof...(Args)>{});
    }

private:
    //function
    std::function<return_type(Args...)> f;
    //parameter in the tuple
    std::tuple<Args...> para;
};

template<class F,typename ...Args>
FuncWrapper<F,Args...> make_function_wapper(F &&f, Args&&...args)
{
    return FuncWrapper<F, Args...>(std::forward<F>(f), std::forward<Args>(args)...);
}

here is the question : I want to create an template class for wrapper function , and use tuple for the parameter store , but it seems that it will not work well and I cannot find why. so I need some help and who can explain for it . thx for your help !

Aucun commentaire:

Enregistrer un commentaire