久久久久久久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>

        如何編寫可變參數模板遞歸函數?

        How to write a variadic template recursive function?(如何編寫可變參數模板遞歸函數?)
          <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>
                • 本文介紹了如何編寫可變參數模板遞歸函數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試編寫一個可變參數模板 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模板網!

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

                  相關文檔推薦

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

                            主站蜘蛛池模板: 日韩专区中文字幕 | 亚洲国产精品自拍 | 日韩a级片| 国产黄色大片 | 一级大片免费看 | www.五月婷婷| 国产色网站 | 色综合久久天天综合网 | 亚洲成人免费视频 | 国产午夜精品视频 | 国产福利视频在线观看 | 国产精品视频一区二区三区 | 一级片网址| 国产精品日韩在线 | 国产精品视频网站 | 天天躁日日躁bbbbb | 一区二区三区免费看 | 久久久亚洲一区 | 亚洲在线观看视频 | 欧美不卡在线 | 男人天堂av网 | 欧美精品乱码99久久蜜桃 | 亚洲一区在线看 | 波多野结衣乳巨码无在线观看 | 亚洲午夜视频在线观看 | 蜜臀久久99精品久久久久久宅男 | 午夜看看 | 亚洲最大av网站 | 精品在线一区 | 蜜桃精品一区二区 | 精品伊人久久 | 精品视频999 | 日本精品视频 | 91成年人 | 看一级黄色片 | 亚洲三区在线观看 | 欧美日韩一区二区在线 | 久久视频在线免费观看 | 欧美日韩小视频 | 亚洲永久免费 | 久艹在线 |