<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 模板別名(正式的名稱為"template typedef") 如何在某個模板類的基礎上,通過寫死(綁定)部分模板參數類型,來定制出一個新的模板類? 嘗嘗我這種寫法如何: ``` template<class T> // 采用了自定義內存分配器的std::vector using Vec = std::vector<T,My_alloc<T>>; // 使用My_alloc為元素分配存儲空間 Vec<int> fib = { 1, 2, 3, 5, 8, 13 }; // verbose與fib類型一致 vector<int,My_alloc<int>> verbose = fib; ``` 有了using語法,定義模板別名則可一目了然:**_using 模板別名 = 引用細節;_**。在這之前,我們也曾徘徊在”typedef”的經典和復雜之間左右為難,但沒有哪一個方案能做到完美平衡,直到后來我們干脆棄而轉向言簡意賅的using語法。 即使模板存在特化,using語法也能照常使用。(需注意:為模板及其各種特化形式可以設定一個統一的別名,但反之則不然:模板特化操作不能通過別名進行)。例如: ``` template<int> // idea: int_exact_trait<N>::type用于表達含有N個bit的數值類型 struct int_exact_traits { typedef int type; }; template<> struct int_exact_traits<8> { typedef char type; }; template<> struct int_exact_traits<16> { typedef char[2] type; }; // ... template<int N> // 定義別名用以簡化書寫 // 譯注:給模板的通用版本取別名,則其所有的特化版本自動獲得該別名, // 例如對于8bit的特化版本,現在可直接使用別名 using int_exact = typename int_exact_traits<N>::type; // int_exact<8> 是含有8個bit的數值類型 int_exact<8> a = 7; ``` 除了在模板方面身擔重任外,using語法也可作為對普通類型定義別名的另一種選擇(相較于typedef更合吾意)。 ``` typedef void (*PFD)(double); // C 樣式 using PF = void (*)(double); // using加上C樣式的類型 using P = [](double)->void; // using和函數返回類型后置語法 ``` 參考: 參考: * the C++ draft: 14.6.7 Template aliases; 7.1.3 The typedef specifier * [N1489=03-0072] Bjarne Stroustrup and Gabriel Dos Reis: [Templates aliases for C++](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1489.pdf) . * [N2258=07-0118] Gabriel Dos Reis and Bjarne Stroustrup: [Templates Aliases (Revision 3)](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf) (final proposal). (翻譯:張瀟,dabaitu)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看