How to write a variadic template recursive function?

后端 未结 6 1199
走了就别回头了
走了就别回头了 2020-12-09 03:39

I\'m trying to write a variadic template constexpr function which calculates sum of the template parameters given. Here\'s my code:

template<         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 04:38

    A more generic solution using std::initializer_list would be:

    template                                                                                                                                                                                          
    auto sum_all(V... v)
    {
      using rettype = typename std::common_type_t;
      rettype result{};
      (void)std::initializer_list{(result += v, 0)...};
      return result;
    }
    

提交回复
热议问题