<i id='mdE6X'><tr id='mdE6X'><dt id='mdE6X'><q id='mdE6X'><span id='mdE6X'><b id='mdE6X'><form id='mdE6X'><ins id='mdE6X'></ins><ul id='mdE6X'></ul><sub id='mdE6X'></sub></form><legend id='mdE6X'></legend><bdo id='mdE6X'><pre id='mdE6X'><center id='mdE6X'></center></pre></bdo></b><th id='mdE6X'></th></span></q></dt></tr></i><div id='mdE6X'><tfoot id='mdE6X'></tfoot><dl id='mdE6X'><fieldset id='mdE6X'></fieldset></dl></div>
  • <small id='mdE6X'></small><noframes id='mdE6X'>

        • <bdo id='mdE6X'></bdo><ul id='mdE6X'></ul>

        <legend id='mdE6X'><style id='mdE6X'><dir id='mdE6X'><q id='mdE6X'></q></dir></style></legend><tfoot id='mdE6X'></tfoot>
      1. 如何编写可变参数模板递归函数?

        时间:2023-08-03

        <legend id='ozLpk'><style id='ozLpk'><dir id='ozLpk'><q id='ozLpk'></q></dir></style></legend>

      2. <tfoot id='ozLpk'></tfoot>
          <i id='ozLpk'><tr id='ozLpk'><dt id='ozLpk'><q id='ozLpk'><span id='ozLpk'><b id='ozLpk'><form id='ozLpk'><ins id='ozLpk'></ins><ul id='ozLpk'></ul><sub id='ozLpk'></sub></form><legend id='ozLpk'></legend><bdo id='ozLpk'><pre id='ozLpk'><center id='ozLpk'></center></pre></bdo></b><th id='ozLpk'></th></span></q></dt></tr></i><div id='ozLpk'><tfoot id='ozLpk'></tfoot><dl id='ozLpk'><fieldset id='ozLpk'></fieldset></dl></div>

          <small id='ozLpk'></small><noframes id='ozLpk'>

                <tbody id='ozLpk'></tbody>
                <bdo id='ozLpk'></bdo><ul id='ozLpk'></ul>
                  本文介绍了如何编写可变参数模板递归函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试编写一个可变参数模板 constexpr 函数来计算给定模板参数的总和.这是我的代码:

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

                  template<int First, int... Rest>
                  constexpr int f()
                  {
                      return First + f<Rest...>();
                  }
                  
                  template<int First>
                  constexpr int f()
                  {
                      return First;
                  }
                  
                  int main()
                  {
                      f<1, 2, 3>();
                      return 0;
                  }
                  

                  不幸的是,它在尝试解析 f<3,>()error C2668: 'f': ambiguous call to重载函数> 打电话.

                  Unfortunately, it does not compile reporting an error message error C2668: 'f': ambiguous call to overloaded function while trying to resolve f<3,>() call.

                  我还尝试将递归基本情况更改为接受 0 个模板参数而不是 1 个:

                  I also tried to change my recursion base case to accept 0 template arguments instead of 1:

                  template<>
                  constexpr int f()
                  {
                      return 0;
                  }
                  

                  但此代码也无法编译(消息 error C2912: explicit specialization 'int f(void)' is not a specialization of a function template).

                  But this code also does not compile (message error C2912: explicit specialization 'int f(void)' is not a specialization of a function template).

                  我可以提取第一个和第二个模板参数来编译和工作,就像这样:

                  I could extract first and second template arguments to make this compile and work, like this:

                  template<int First, int Second, int... Rest>
                  constexpr int f()
                  {
                      return First + f<Second, Rest...>();
                  }
                  

                  但这似乎不是最好的选择.那么,问题是:如何以优雅的方式编写此计算?

                  But this does not seem to be the best option. So, the question is: how to write this calculation in an elegant way?

                  UP:我也试着把它写成一个单一的函数:

                  UP: I also tried to write this as a single function:

                  template<int First, int... Rest>
                  constexpr int f()
                  {
                      return sizeof...(Rest) == 0 ? First : (First + f<Rest...>());
                  }
                  

                  这也不起作用:error C2672:'f':找不到匹配的重载函数.

                  推荐答案

                  您的基本情况是错误的.您需要一个空列表的案例,但正如编译器所建议的那样,您的第二次尝试不是有效的模板专业化.为零参数定义有效实例化的一种方法是创建一个接受空列表的重载

                  Your base case was wrong. You need a case for the empty list, but as the compiler suggests, your second try was not a valid template specialization. One way to define a valid instantiation for zero arguments is to create an overload that accepts an empty list

                  template<class none = void>
                  constexpr int f()
                  {
                      return 0;
                  }
                  template<int First, int... Rest>
                  constexpr int f()
                  {
                      return First + f<Rest...>();
                  }
                  int main()
                  {
                      f<1, 2, 3>();
                      return 0;
                  }
                  

                  <小时>

                  为了完整起见,也是我的第一个答案,@alexeykuzmin0 通过添加条件来修复:


                  for completeness sake also my first answer, that @alexeykuzmin0 fixed by adding the conditional:

                  template<int First=0, int... Rest>
                  constexpr int f()
                  {
                      return sizeof...(Rest)==0 ? First : First + f<Rest...>();
                  }
                  

                  这篇关于如何编写可变参数模板递归函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:作为函数参数传递的数组的大小 下一篇:通过泛型 lambda 理解 Y Combinator

                  相关文章

                  最新文章

                • <i id='Ui7uP'><tr id='Ui7uP'><dt id='Ui7uP'><q id='Ui7uP'><span id='Ui7uP'><b id='Ui7uP'><form id='Ui7uP'><ins id='Ui7uP'></ins><ul id='Ui7uP'></ul><sub id='Ui7uP'></sub></form><legend id='Ui7uP'></legend><bdo id='Ui7uP'><pre id='Ui7uP'><center id='Ui7uP'></center></pre></bdo></b><th id='Ui7uP'></th></span></q></dt></tr></i><div id='Ui7uP'><tfoot id='Ui7uP'></tfoot><dl id='Ui7uP'><fieldset id='Ui7uP'></fieldset></dl></div>

                      <tfoot id='Ui7uP'></tfoot>
                    1. <legend id='Ui7uP'><style id='Ui7uP'><dir id='Ui7uP'><q id='Ui7uP'></q></dir></style></legend>
                      • <bdo id='Ui7uP'></bdo><ul id='Ui7uP'></ul>

                      <small id='Ui7uP'></small><noframes id='Ui7uP'>