最簡單的C++模板範例

來源:互聯網
上載者:User

  1. // MaxTemplate.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7. template <typename T>
  8. inline T const& max(T const& a, T const& b)
  9. {
  10.     return a > b ? a : b; 
  11. }
  12. //test
  13. int main()
  14. {
  15.     int a = 0;
  16.     int b = 0;
  17.     
  18.     //給a b 賦值為隨機數
  19.     a = ::rand();
  20.     b = ::rand();
  21.     cout << "max(" << a << ", " << b << ") = " << ::max(a, b) << endl;
  22.     double du1 = 0.0;
  23.     double du2 = 0.0;
  24.     du1 = (double)rand() / 10000;
  25.     du2 = (double)rand() / 10000;
  26.     cout << "max(" << du1 << ", " << du2 << ") = " << ::max(du1, du2) << endl;
  27.     string str1 = "hello";
  28.     string str2 = "test";
  29.     cout << "max(" << str1 << ", " << str2 << ") = " << ::max(str1, str2) << endl;
  30.     return 0;
  31. }

程式調用了 max()三次。第一次所給自變數是兩個 int,第二次所給自變數是兩個 double,最後一 次給的是兩個 std::string。每一次 max()均比較兩值取其大者。程式運行結果為:max(7,i): 42max(f1,f2): 3.4max(s1,s2): mathematics注意程式對max()的三次調用都加了首碼字 "::",以便確保被調用的是我們在全域命名空間(global namespace)中定義的 max()。標準庫內也有一個 std::max() template,可能會在某些情況下被調用,或在調用時引發模稜兩可(ambiguity,歧義性)。一般而言,templates 不會被編譯為「能夠處理任意類型」的單一實體(entity),而是被編譯為多個個別實體,每一個處理某一特定類型。因此,針對三個類型,max()被編譯成三個實體。 例如第一次調用 max():int i = 42;    max(7,i)    使用的是「以int 為 template parameter T」的 function template,語意上等同於調用以下函數:inline int const& max (int const& a, int const& b){// 如果 a<b 就傳回 b,否則傳回 areturn a < b ? b : a;}以具體類型替換 template parameters 的過程稱為「執行個體化」(instantiation,或稱「實體化」)。過程中會產生 template 的一份實體(instance)。不巧的是,instantiation(執行個體化、執行個體化產品)和instance(實體)這兩個術語在OO(物件導向)編程領域中有其它含義,通常用來表示一個 class的具體對象(concrete object)。本書專門討論 templates,因此當我們運用這個術語時,除非另有明確指示,表達的是 templates 方面的含義。注意,只要 function template 被使用,就會自動引發執行個體化過程。程式員沒有必要個別申請執行個體化過程。類似情況,另兩次對 max()的調用被執行個體化為:const double& max (double const&, double const&);const std::string& max (std::string const&, std::string const&);如果試圖以某個類型來執行個體化 function template,而該類型並未支援 function template 中用到的操作,就會導致編譯錯誤。例如:std::complex<float> c1, c2; // 此類型並不提供 operator<   max(c1,c2); // 編譯期出錯實際上,templates 會被編譯兩次:1. 不執行個體化,只是對 template 程式碼進行語法檢查以發現諸如「缺少分號」等等的語法錯誤。2. 執行個體化時,編譯器檢查 template 程式碼中的所有調用是否合法,諸如「未獲支援之函數調用」便會在這個階段被檢查出來。這會導致一個嚴重問題:當 function template 被運用而引發執行個體化過程時,某些時候編譯器需要用到template 的原始定義。一般情況下,對普通的(non-template)functions而言,編譯和連結兩步驟是各自獨立的,編譯器只檢查各個functions的聲明語句是否和調用語句相符,然而template 的編譯破壞了這個規則。解決辦法在第6章討論。眼下我們可以用最簡單的解法:把template 程式碼以 inline 形式寫在標頭檔(header)中。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.