特化或全特化時,template后面的尖括號中不帶任何內容,為什么感覺iterator_traits沒有遵守呢?
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;
};
template <class _Tp>
struct iterator_traits<_Tp*> {
typedef random_access_iterator_tag iterator_category;
typedef _Tp value_type;
typedef ptrdiff_t difference_type;
typedef _Tp* pointer;
typedef _Tp& reference;
};
template <class _Tp>
struct iterator_traits<const _Tp*> {
typedef random_access_iterator_tag iterator_category;
typedef _Tp value_type;
typedef ptrdiff_t difference_type;
typedef const _Tp* pointer;
typedef const _Tp& reference;
};
按照《c++primer》的說法,類模板在特化的時候,template<>之后要為空引數,為什么標準庫沒有這么寫呢?仍然加了引數而不是空引數
uj5u.com熱心網友回復:
template <class _Tp>struct iterator_traits<_Tp*>特化為某一泛化型別時,是要加上 class _Tp
template <> struct iterator_traits<int *>
這種 template<> 是特化一個具體的型別。
而特化一系列型別時,就要寫上了 template<class t >
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/119899.html
標籤:基礎類
