| Author: | David Abrahams |
|---|---|
| Contact: | dave@boost-consulting.com |
| Organization: | Boost Consulting |
| Date: | 2006-09-11 |
| Copyright: | Copyright David Abrahams 2004. |
| 概要: | 頭文件 <boost/iterator/iterator_traits.hpp> 提供了使用MPL兼容的 metafunctions元函數 來訪問一個迭代器的關聯類型的能力。 |
|---|
std::iterator_traits 提供了對任意迭代器的五個關聯類型的訪問:它們是 value_type, reference, pointer, iterator_category, 和 difference_type. 不幸的是,像 "multi-valued" 這樣的 traits 模板很難用於元編程的上下文中。<boost/iterator/iterator_traits.hpp> 提供了使用標準 元函數 來訪問這些類型的方法。
頭文件 <boost/iterator/iterator_traits.hpp>:
template <class Iterator>
struct iterator_value
{
typedef typename
std::iterator_traits<Iterator>::value_type
type;
};
template <class Iterator>
struct iterator_reference
{
typedef typename
std::iterator_traits<Iterator>::reference
type;
};
template <class Iterator>
struct iterator_pointer
{
typedef typename
std::iterator_traits<Iterator>::pointer
type;
};
template <class Iterator>
struct iterator_difference
{
typedef typename
detail::iterator_traits<Iterator>::difference_type
type;
};
template <class Iterator>
struct iterator_category
{
typedef typename
detail::iterator_traits<Iterator>::iterator_category
type;
};
由於 Boost 中所用的技術,你可能發現這些 元函數 會比你的編譯器的標準庫所提供的工具更為好用。
在那些不支持偏特化的編譯器上,如 Microsoft Visual C++ 6.0 或 7.0, 你可能需要手工對那些傳給以上元函數的指針的 value_type 調用 BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION .
由於 GCC-2.9x 中的 bug, 在此編譯器上 iterator_category 的名字被改為 iterator_category_. 宏 BOOST_ITERATOR_CATEGORY 會依據不同的平台被擴展為 iterator_category 或 iterator_category_, 以實現可移植性。