久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

  • <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 class="qwawimqqmiuu" id='ozLpk'><tfoot id='ozLpk'></tfoot><dl id='ozLpk'><fieldset id='ozLpk'></fieldset></dl></div>

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

          <bdo id='ozLpk'></bdo><ul id='ozLpk'></ul>

        如何編寫可變參數(shù)模板遞歸函數(shù)?

        How to write a variadic template recursive function?(如何編寫可變參數(shù)模板遞歸函數(shù)?)
          <tbody id='mdE6X'></tbody>
        <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 class="qwawimqqmiuu" id='mdE6X'><tfoot id='mdE6X'></tfoot><dl id='mdE6X'><fieldset id='mdE6X'></fieldset></dl></div>
      1. <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>
                • 本文介紹了如何編寫可變參數(shù)模板遞歸函數(shù)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我正在嘗試編寫一個可變參數(shù)模板 constexpr 函數(shù)來計算給定模板參數(shù)的總和.這是我的代碼:

                  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重載函數(shù)> 打電話.

                  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 個模板參數(shù)而不是 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).

                  我可以提取第一個和第二個模板參數(shù)來編譯和工作,就像這樣:

                  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...>();
                  }
                  

                  但這似乎不是最好的選擇.那么,問題是:如何以優(yōu)雅的方式編寫此計算?

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

                  UP:我也試著把它寫成一個單一的函數(shù):

                  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':找不到匹配的重載函數(shù).

                  推薦答案

                  您的基本情況是錯誤的.您需要一個空列表的案例,但正如編譯器所建議的那樣,您的第二次嘗試不是有效的模板專業(yè)化.為零參數(shù)定義有效實例化的一種方法是創(chuàng)建一個接受空列表的重載

                  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 通過添加條件來修復(fù):


                  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...>();
                  }
                  

                  這篇關(guān)于如何編寫可變參數(shù)模板遞歸函數(shù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  In what ways do C++ exceptions slow down code when there are no exceptions thown?(當(dāng)沒有異常時,C++ 異常會以何種方式減慢代碼速度?)
                  Why catch an exception as reference-to-const?(為什么要捕獲異常作為對 const 的引用?)
                  When and how should I use exception handling?(我應(yīng)該何時以及如何使用異常處理?)
                  Scope of exception object in C++(C++中異常對象的范圍)
                  Catching exceptions from a constructor#39;s initializer list(從構(gòu)造函數(shù)的初始化列表中捕獲異常)
                  Difference between C++03 throw() specifier C++11 noexcept(C++03 throw() 說明符 C++11 noexcept 之間的區(qū)別)
                • <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 class="qwawimqqmiuu" id='Ui7uP'><tfoot id='Ui7uP'></tfoot><dl id='Ui7uP'><fieldset id='Ui7uP'></fieldset></dl></div>

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

                              <tbody id='Ui7uP'></tbody>

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

                            主站蜘蛛池模板: 毛片com| 亚洲品质自拍视频网站 | 一二区电影 | 国产成人精品久久二区二区91 | 日韩精品在线一区 | 色综合色综合网色综合 | 在线国产视频 | 婷婷色国产偷v国产偷v小说 | 亚洲精品在| 日韩视频免费看 | 95国产精品 | 美女久久久久久久 | 日韩av在线中文字幕 | 国产日韩一区二区三区 | 黄色一级免费 | 国产九九九九 | 成人国产a | 少妇午夜一级艳片欧美精品 | 精品一区二区三区在线观看国产 | 粉嫩一区二区三区国产精品 | 很很干很很日 | 日韩视频在线免费观看 | 日日夜夜视频 | 欧美区在线 | 水蜜桃久久夜色精品一区 | 亚洲欧美一区二区三区视频 | 国产精品欧美一区二区 | 九九热在线免费视频 | 日韩在线观看 | 国产精品日日做人人爱 | 中文字幕第二十页 | 91国产视频在线 | 天天综合永久入口 | 久夜精品 | 精品二 | 国产精品久久久久久久久久久久冷 | 黄色激情毛片 | 国产精品99久久久久久动医院 | 国产精品视频中文字幕 | 欧美日韩国产综合在线 | 国产大片黄色 |