針對類型資訊的特化

來源:互聯網
上載者:User

類模板顯式特化和局部特化通常用於產生主類模板的一些版本,這些版本根據具體的模板模板實參或模板實參的類定製而成。

然而,這些語言特性常常也被以相反的樣式使用,即,不是基於類型的屬性產生特化版本,而是從一個特化版本中推匯出類型的屬性。

例:

template<class T>
struct IsInt  //T 不是一個 int
{
 enum { result = false };
};

template<>
struct IsInt<int> //除非T 是一個int
{
 enum {result = true};
};

template<typename X>
void Test(const X &arg)
{
 if (IsInt<X>::result)
     std::cout<<arg<<"is integer/n";
 else
  std::cout<<arg<<"is not integer"<<"/n";
}

int _tmain(int argc, _TCHAR* argv[])
{

 int a = 10;
 Test<int>(a);
 system("pause");
 return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.