Boost.Iterator 庫 Boost


Authors: David Abrahams, Jeremy Siek, Thomas Witt
Contact: dave@boost-consulting.com, jsiek@osl.iu.edu, witt@styleadvisor.com
organizations: Boost Consulting, Indiana University Open Systems Lab, Zephyr Associates, Inc.
date: $Date: 2008-03-22 17:45:55 -0400 (Sat, 22 Mar 2008) $
copyright: Copyright David Abrahams, Jeremy Siek, Thomas Witt 2003.
概要: Boost Iterator 庫包含兩部分。第一個部分是一個 概念 系統,擴展了 C++ 標準的迭代器需求。第二個部分是一個組件框架,基於這些擴展概念構建了一些迭代器,還包括幾個有用的迭代器適配器。這些擴展的迭代器概念是精心設計的,舊 式的迭代器也可以適合於新的概念,新式的迭代器兼容於舊式的算法,不過如果你想充分利用新式迭代器的功能,可能需要對算法進行一些修改。本庫中的幾個組件 已經被接納進入 C++ 標準技術報告。Boost Iterator 庫的組件將替代舊的 Boost Iterator Adaptor 庫。

目錄

  • 新式迭代器
  • 迭代器外觀與適配器
  • 特定的適配器
  • 迭代器工具
    • Traits
    • 測試和概念檢查
  • 從舊的 Boost Iterator Adaptor 庫升級
  • 歷史

新式迭代器

C++98 中定義的迭代器類別非常有限,這是因為它們將兩個直交的概念:遍歷和元素訪問,綁定在了一起。例如,因為隨機訪問迭代器要求在提領時返回一個引用(而不是一個代理),這使得不可能用 C++98 中的迭代器類別來獲得 vector<bool>::iterator 的能力。這就是聲名狼籍的 "vector<bool> 不是一個容器,其迭代器也不是隨機訪問迭代器",有關於此,Herb Sutter 為標準委員會寫了兩篇文件(n1185 和 n1211),以及一個 Guru of the Week. 新式迭代器很好地修補了 vector<bool>, 雖然:還有大量其它在用的迭代器不能用已有的概念充分地表示出來。有關新的迭代器概念的詳細,請看

關於新式迭代器的標準建議書 (PDF)

迭代器外觀和適配器

編寫符合標準的迭代器是需要一些技巧的,但這種需要常常被提出。為了更易於實現新的迭代器,Boost.Iterator 庫提供了 iterator_facade 類模板,它實現了許多有用的缺省值和編譯期檢查,被設計為幫助迭代器的作者保證他的迭代器是正確的。

還有一種情況常常會遇到,即定義的新迭代器類似於某個底層的迭代器或某種類-迭代器的類型,不過要對底層類型某些方面的行為進行一些修改。為了這個目的,本庫提供了 iterator_adaptor 類模板,它被設計為盡可能地利用底層類型的行為。

這兩個類的相關文檔請見以下 web 頁:

  • iterator_facade (PDF)
  • iterator_adaptor (PDF)

iterator_facade 和 iterator_adaptor 以及許多下面提及的 特定適配器 均已被提議進入標準,並被接納進入第一版的 C++ 技術報告;更多詳情請見

關於迭代器的外觀和適配器的標準建議書 (PDF)


特定的迭代器

iterator 庫基於 Boost iterator 外觀和適配器 提供了一組非常有用的,符合標準的迭代器模板:.

  • counting_iterator (PDF): 一個相鄰值序列上的迭代器。實現了 "lazy sequence"。
  • filter_iterator (PDF): 某個序列中滿足給定條件的元素組成的子序列上的迭代器。
  • function_output_iterator (PDF): 包裝一個單參函數對象的輸出迭代器;每次一個元素被寫入提領迭代器時,該元素被作為參數傳入函數對象。
  • indirect_iterator (PDF): 由指向某個序列中元素的對象所組成的序列上的迭代器。
  • permutation_iterator (PDF): 某個隨機訪問序列的元素之上的迭代器,依據某個整數索引序列進行重排。
  • reverse_iterator (PDF): 以相反順序遍歷某個雙向序列的迭代器。修正了 C++98 中的 std::reverse_iterator 的許多缺點。
  • shared_container_iterator: 某個容器的迭代器,該容器的元素的生命週期由保存在迭代器中的一個 shared_ptr 管理。
  • transform_iterator (PDF): 對底層序列的元素進行某種功能轉換後,所得結果的迭代器。該組件可以替代舊的 projection_iterator_adaptor.
  • zip_iterator (PDF): 由不同種類的底層迭代器在相應位置所組成的 tuples 的迭代器。

迭代器工具

Traits

  • pointee.hpp (PDF): 以泛化代碼提供了推斷指針、智能指針和迭代器所指物的類型的能力。用於 indirect_iterator.
  • iterator_traits.hpp (PDF): 提供了 MPL-兼容的元函數,用於提取迭代器的 traits. 同時也修正了 std::iterator_traits 實現中的不足。

測試和概念檢查

  • iterator_concepts.hpp (PDF): 用於新的迭代器概念的概念檢查類。
  • iterator_archetypes.hpp (PDF): 用於新的迭代器概念的概念原型類。

從舊的 Boost Iterator Adaptor 庫升級

如果你已經使用了舊的 Boost Iterator Adaptor 庫來實現迭代器,也許你編寫了一個 Policies 類來提供你的迭代器的核心操作。在這個新的迭代器設計中,你要將相同的核心操作移到迭代器類本身裡面。如果你寫了一組迭代器,那麼也許你寫了一個 type generator 來構建所需的 iterator_adaptor 特化物;在新的庫中,你不需要類型生成器(雖然可能為了兼容舊的代碼而保留它),由於 Curiously Recurring Template Pattern (CRTP) [Cop95] 的使用,你現在可以定義自己的迭代器類並通過從 iterator_facade 或 iterator_adaptor 派生來獲得所需的功能。結果是,你可以更好地控制你的迭代器如何工作:你可以增加額外的構造函數,或者覆寫由庫提供的迭代器功能。

如果你在查找舊的 projection_iterator 組件,那麼它的功能已經被合併入 transform_iterator: 只要函數對象的 result_type (或是 Reference 模板參數,如果它被顯式指定)是一個真的引用類型,那麼 transform_iterator 將和具有和 projection_iterator 一樣的行為。

歷史

In 2000 Dave Abrahams was writing an iterator for a container of pointers, which would access the pointed-to elements when dereferenced. Naturally, being a library writer, he decided to generalize the idea and the Boost Iterator Adaptor library was born. Dave was inspired by some writings of Andrei Alexandrescu and chose a policy based design (though he probably didn't capture Andrei's idea very well - there was only one policy class for all the iterator's orthogonal properties). Soon Jeremy Siek realized he would need the library and they worked together to produce a "Boostified" version, which was reviewed and accepted into the library. They wrote a paper and made several important revisions of the code.

Eventually, several shortcomings of the older library began to make the need for a rewrite apparent. Dave and Jeremy started working at the Santa Cruz C++ committee meeting in 2002, and had quickly generated a working prototype. At the urging of Mat Marcus, they decided to use the GenVoca/CRTP pattern approach, and moved the policies into the iterator class itself. Thomas Witt expressed interest and became the voice of strict compile-time checking for the project, adding uses of the SFINAE technique to eliminate false converting constructors and operators from the overload set. He also recognized the need for a separate iterator_facade, and factored it out of iterator_adaptor. Finally, after a near-complete rewrite of the prototype, they came up with the library you see today.

[Cop95] [Coplien, 1995] Coplien, J., Curiously Recurring Template Patterns, C++ Report, February 1995, pp. 24-27.

View document source. Generated by Docutils from reStructuredText source.