<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          TmplArgs函數(shù)參數(shù)分析程序

          聯(lián)合創(chuàng)作 · 2023-09-22 08:07

          TmplArgs,使用模板技術分析函數(shù)參數(shù),從而在編譯期得到更多信息和條件。

          我們可以通過函數(shù)重載為同名函數(shù)實現(xiàn)不同參數(shù)個數(shù)的版本,如:

          int max(int a, int b) { return a>b?a:b; }
          int max(int a, int b, int c)
          { return max(a,b)>max(b,c)?max(a,b):max(b,c); }

          我們還可以通過使用函數(shù)模板來擴展函數(shù)對參數(shù)類型的定制,如:

          template<typename _T>
          void debug(_T a, std::string b) { cout<<a<<b<<endl; }
          template<typename _T, typename _T1>
          void debug(_T a, _T1 b, std::string c) { cout<<a<<b<<c<<endl; }

          然而這種規(guī)則對于類模板卻并不是(至少現(xiàn)在不是)奏效的。如:

          template<typename _T> struct Debug {
              _T _a;
              Debug(_T a) : _a(a) {}
              void debug(std::string s) { cout<<_a<<s; }
          };
          template<typename _T, typename _T1> struct Debug {
              _T _a;
              _T1 _b;
              Debug(_T a, _T1 b) : _a(a), _b(b) {}
              void debug(std::string s) { cout<<_a<<_b<<s; }
          };

          上面的代碼是不符合編譯器(VS2010)規(guī)則的。也就是說我們沒有辦法為同名的類模板實現(xiàn)不同參數(shù)的版本。 那么我們有沒有辦法突破這個限制呢?是的,通過 TmplArgs 庫,我們便獲得了這種能力。

          雖然 TmplArgs 的使用難度已經(jīng)降到了最低,但是它仍然不完美:
          首先,我們需要實現(xiàn)類模板的宏定義式,如 DebugBase.h。
          然后,我們需要實現(xiàn)類模板的通用形式,如 Debug.h。
          最后,我們還需要將參數(shù)規(guī)則寫成函數(shù)指針的形式。

          下面這段代碼就是使用 TmplArgs 后對上面的 Debug 模板的改寫:

          Debug<void(int)> a;
          a.debug(3,"0");
          Debug<void(int,string)> b;
          b.debug(3, "4", "0");
          瀏覽 18
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天撸视频 夜夜撸视频 | 青娱乐在线电影 | 欧美a网站 | 俺去也俺就去www色情网最新更新内容 | 久草福利视频 |