<轉>詳解C++的模板中typename關鍵字的用法


用處1, 用在模板定義里, 標明其后的模板參數是類型參數。

例如:

template<typename T, typename Y>
T foo(const T& t, const Y& y){//....};

templace<typename T>
class CTest
{
private:
 T t;
public:
 //...
}

其實,這里最常用的是使用關鍵字class,而且二者功能完全相同,這里的class和定義類時的class完全是兩回事,C++當時就是為了減少關鍵字,才使用了class。但最終卻不得不引入了typename,究竟是

什么原因呢?請看第二條,也就是typename的第二個用法。

用處2, 模板中標明“內嵌依賴類型名”

這里有三個詞,內嵌、依賴、類型名。那么什么是“內嵌依賴類型名(nested dependent type name)”?

請看SGI STL里的一個例子, 只是STL中count范型算法的實現:

template <class _InputIter, class _Tp>
typename iterator_traits<_InputIter>::difference_type
count(_InputIter __first, _InputIter __last, const _Tp& __value) {
 __STL_REQUIRES(_InputIter, _InputIterator);
 __STL_REQUIRES(typename iterator_traits<_InputIter>::value_type,
         _EqualityComparable);
 __STL_REQUIRES(_Tp, _EqualityComparable);
 typename iterator_traits<_InputIter>::difference_type __n = 0;
 for ( ; __first != __last; ++__first)
  if (*__first == __value)
   ++__n;
 return __n;
}

這里有三個地方用到了typename:返回值、參數、變量定義。分別是:

typename iterator_traits<_InputIter>::difference_type
typename iterator_traits<_InputIter>::value_type
typename iterator_traits<_InputIter>::difference_type __n = 0;

difference_type, value_type就是依賴於_InputIter(模板類型參數)的類型名。源碼如下:

template <class _Iterator>
struct iterator_traits {
 typedef typename _Iterator::iterator_category iterator_category;
 typedef typename _Iterator::value_type    value_type;
 typedef typename _Iterator::difference_type  difference_type;
 typedef typename _Iterator::pointer      pointer;
 typedef typename _Iterator::reference     reference;
};

內嵌是指定義在類名的定義中的。以上difference_type和value_type都是定義在iterator_traits中的。

依賴是指依賴於一個模板參數。 typename iterator_traits::difference_type中difference_type依賴於模板參數_InputIter。

類型名是指這里最終要指出的是個類型名,而不是變量。例如iterator_traits::difference_type完全有可能是類iterator_traits 類里的一個static對象。而且當我們這樣寫的時候,C++默認就是解釋為一個變量的。所以,為了和變量區分,必須使用typename告訴編譯器。
那么是不是所有的T::type_or_variable, 或者tmpl:type_or_variable都需要使用typename呢?不是,有以下兩個例外。

例外

(1)類模板定義中的基類列表。

例如

template<class T>
class Derived: public Base<T>::XXX
{
  ...
}

(2)類模板定義中的初始化列表。

Derived(int x) : Base<T>::xxx(x)
{
  ...
}

為什么這里不需要呢?因為編譯器知道這里需要的是類型還是變量,(1)基類列表里肯定是類型名,(2)初始化列表里肯定是成員變量名。

typename和class的區別

在c++ Template中很多地方都用到了typename與class這兩個關鍵字,而且好像可以替換,是不是這兩個關鍵字完全一樣呢?

相信學習C++的人對class這個關鍵字都非常明白,class用於定義類,在模板引入c++后,最初定義模板的方法為: template<class T>……

在這里class關鍵字表明T是一個類型,后來為了避免class在這兩個地方的使用可能給人帶來混淆,所以引入了typename這個關鍵字,它的作用同

class一樣表明后面的符號為一個類型,這樣在定義模板的時候就可以使用下面的方式了:

template<typename T>……

在模板定義語法中關鍵字class與typename的作用完全一樣。

typename難道僅僅在模板定義中起作用嗎?其實不是這樣,typename另外一個作用為:使用嵌套依賴類型(nested depended name),如下所示:

class MyArray 
{ 
  public:
  typedef int LengthType;
  .....
}

template<class T>
void MyMethod( T myarr ) 
{ 
  typedef typename T::LengthType LengthType; 
  LengthType length = myarr.GetLength; 
}

這個時候typename的作用就是告訴c++編譯器,typename后面的字符串為一個類型名稱,而不是成員函數或者成員變量,這個時候如果前面沒有typename,編譯器沒有任何辦法知道T::LengthType是一個類型還是一個成員名稱(靜態數據成員或者靜態函數),所以編譯不能夠通過。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM