Call function with part of variadic arguments

前端 未结 4 746
傲寒
傲寒 2021-01-22 10:42

Consider I have the following:

void bar(int a, int b)
{
}   

template
void foo(F function, Args... args>
{
    function(a         


        
4条回答
  •  醉梦人生
    2021-01-22 11:37

    Trivial and hardly extensible solution would be to create a wrapper, that will be called with all arguments, but will use only first few of them.

    template
    void foo(F function, Args... args)
    {
        // with proper forwarding if needed
        auto lambda = [](auto fnc, auto first, auto second, auto...)
        {
            fnc(first, second);
        };
        lambda(function, args...);
    }
    

提交回复
热议问题