C++ variadic template function parameter with default value

前端 未结 2 1747
忘掉有多难
忘掉有多难 2021-02-07 02:30

I have a function which takes one parameter with a default value. Now I also want it to take a variable number of parameters and forward them to some other function. Function pa

2条回答
  •  你的背包
    2021-02-07 02:41

    Another approach would be to pass variadic arguments through a tuple.

    template 
    void func (std::tuple t, SomeSpecialType num = fromNum(5))
    {
      // don't forget to move t when you use it for the last time
    }
    

    Pros : interface is much simpler, overloading and adding default valued arguments is quite easy.

    Cons : caller has to manually wrap arguments in a std::make_tuple or std::forward_as_tuple call. Also, you'll probably have to resort to std::index_sequence tricks to implement the function.

提交回复
热议问题