Feed template function element from tuple at runtime?

前端 未结 2 541
失恋的感觉
失恋的感觉 2021-01-20 19:37

In C++ I have a tuple with some elements in it:

std::tuple  my_tuple(3, \'q\');

And some template function that perfectly

2条回答
  •  臣服心动
    2021-01-20 19:57

    Following may help:

    template  struct caller;
    
    template  struct caller>
    {
        template 
        void operator() (F f, std::tuple& t, int n)
        {
            (*this)(f, t, n, std::index_sequence_for());
        }
    private:
        template 
        void operator() (F f, std::tuple& t, int n, std::index_sequence)
        {
            std::function&)> fs[] = { &helper... };
            fs[n](f, t);
        }
    
        template 
        static void helper(F f, std::tuple& t)
        {
            f(std::get(t));
        }
    
    };
    
    template 
    void call(F f, T& t, int n)
    {
        caller()(f, t, n);
    }
    

    Live example

提交回复
热议问题