看板 PLT 關於我們 聯絡資訊
網路快多了 XD 剛剛打到有點想翻桌 ※ 引述《godfat (godfat 真常)》之銘言: : 但在這個例子中,最佳化良好的話,其實一個 class 都不會產生。 : 因為 compiler 會發現 fact<10>::value 是個常數, : 而其他地方都沒有用到 fact<...>..., : 所以 linking 時就會全部捨棄了。 回到我們有長度的 list 上,這邊就寫成: using std::list; using std::size_t; // usually is unsigned int template <class T, size_t N> struct List: public list<T>{ List(): list<T>(N){} // for size N list List(T t[N]): list<T>(t, t+N){} // init up the data }; 暫時先忽略幾件事,size_t 是為了跨平台而定的名稱, 通常他就只是 unsigned int. std::list 是標準實作的 list, 暫時當他是只有一個 argument 的 template, 像是 list<int> 就是一個存 int 的 list.(後面還有 allocator 的參數) 直接繼承 std::list 是為了運用他的實作,這裡只幫他追加長度的屬性。 struct 的意思跟 class 近似,只差在預設使用 public. 所以現在 List<int, 5> 就會是一個存有 5 個 int 的 List, 假設寫: List<int, 5> a; List<int, 7> b; a = b; 這樣是不行的,因為 List<int, 5> 和 List<int, 7> 在 runtime 是「完全」 不同的東西。不過這當然不是意味著他們無法混著使用,因為在 compile-time 還是可以靠著 template 去玩他們。 比方說來定義個 concat, 可以這樣寫: template <class T, size_t M, size_t N> List<int, M+N> concat(List<T, M> const& xs, List<T, N> const& ys){ List<int, M+N> result; // ...自己想辦法把 xs 和 ys 存到 result 中 return result; } List<int, 12> c = concat(a, b); 碰到這裡,compiler 就會開始利用 a 與 b 的 type 找到 concat, 會發現 T = int, M = 5, N = 7 於是推斷出 return type 會是 List<int, 12>, 再藉由此 type 跟 c 的 type 比對。 結果當然是吻合(不吻合就會有 type error),於是 concat 就能傳回正確的資料。 至少長度正確... list 內容是 runtime 的東西,template 就幫不上忙了。 不過 boost 有個 MPL, meta-programming library, 有實作 compile-time 的 list, 那個東西就真的能檢查內容正確性了。 只是 template 玩成這樣真的是會很累就是了... 畢竟原本不是設計來搞這種東西的。 * 除了整數以外,template 其實也可以放各種 constant address,如: struct Dummy{int i;}; Dummy dummy; 我們知道 global object 的 address 本身是 constant, 也就是 &dummy 會是 const. template <Dummy* D> struct Mummy{ static Dummy* value; // static 成員的初始化要寫在外面 }; template <Dummy* D> Dummy* Mummy<D>::value = D; // 這裡 於是可以這樣去存取: Mummy<&dummy>::value->i = 10; 我也不知道這樣做能幹嘛就是了...目前好像還沒看過有人利用這種特性。 C++ template 就講到這 @_@ -- In Lisp, you don't just write your program down toward the language, you also build the language up toward your program. 《Programming Bottom-Up》- Paul Graham 1993 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.135.28.18