[英]Template typename
C++ standard
是否在以下聲明中指定T
可以表示什么:
template <typename T>
我的意思是,從實際的角度來看,它可以是任何特定類型,允許模板進行編譯(在發生相應替換時) 。
但是嚴格的定義呢?
您想要的標准是:
C ++ 03、14.1,模板參數:
A template defines a family of classes or functions.
template-declaration:
exportopt template < template-parameter-list > declaration
template-parameter-list:
template-parameter
template-parameter-list , template-parameter
template-parameter:
type-parameter
parameter-declaration
type-parameter:
class identifieropt
class identifieropt = type-id
typename identifieropt
typename identifieropt = type-id
template < template-parameter-list > class identifieropt
template < template-parameter-list > class identifieropt = id-expression
..
在模板聲明的范圍內,類型參數將其標識符定義為類型名稱(如果使用類或類型名稱聲明)或模板名稱(如果使用模板聲明)。
..
如果在模板專門化的實例化中使用模板參數導致格式不正確的構造,則程序格式不正確。
其他的事情是默認參數,非類型模板等。換句話說,標准不說任何東西 T
。
程序員有責任確保用於T
的數據類型兼容並且定義了將在T
執行的所有必要操作。 就C ++標准而言,可以在其中使用任何數據類型來代替T
boost有一個有用的模板enable_if,它使您可以僅為特定類型啟用模板。
沒有嚴格的定義,因為這似乎與模板的實際目的背道而馳。 T
是任何類型,例如,作為參數傳遞給具有T
類型參數的函數。
為了模板的代碼可重用性,您犧牲了嚴格類型定義的安全性。 有了這種自由,您需要提供檢查以確保T
是該函數的合理類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.