concepts.qbk 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. [section:concepts Iterator Concepts]
  2. [section:access Access]
  3. [section:readable Readable Iterator Concept]
  4. A class or built-in type `X` models the *Readable Iterator* concept
  5. for value type `T` if, in addition to `X` being Assignable and
  6. Copy Constructible, the following expressions are valid and respect
  7. the stated semantics. `U` is the type of any specified member of
  8. type `T`.
  9. [table Readable Iterator Requirements (in addition to Assignable and Copy Constructible)
  10. [
  11. [Expression]
  12. [Return Type]
  13. [Note/Precondition]
  14. ]
  15. [
  16. [`iterator_traits<X>::value_type`]
  17. [`T`]
  18. [Any non-reference, non cv-qualified type]
  19. ]
  20. [
  21. [`*a`]
  22. [ Convertible to `T`]
  23. [pre: `a` is dereferenceable. If `a == b` then `*a` is equivalent to `*b`.]
  24. ]
  25. [
  26. [`a->m`]
  27. [`U&`]
  28. [pre: `(*a).m` is well-defined. Equivalent to `(*a).m`.]
  29. ]
  30. ]
  31. [endsect]
  32. [section:writable Writable Iterator Concept]
  33. A class or built-in type `X` models the *Writable Iterator* concept
  34. if, in addition to `X` being Copy Constructible, the following
  35. expressions are valid and respect the stated semantics. Writable
  36. Iterators have an associated *set of value types*.
  37. [table Writable Iterator Requirements (in addition to Copy Constructible)
  38. [
  39. [Expression]
  40. [Return Type]
  41. [Precondition]
  42. ]
  43. [
  44. [`*a = o` ]
  45. []
  46. [pre: The type of `o` is in the set of value types of `X`]
  47. ]
  48. ]
  49. [endsect]
  50. [section:swappable Swappable Iterator Concept]
  51. A class or built-in type `X` models the *Swappable Iterator* concept
  52. if, in addition to `X` being Copy Constructible, the following
  53. expressions are valid and respect the stated semantics.
  54. [table Swappable Iterator Requirements (in addition to Copy Constructible)
  55. [
  56. [Expression]
  57. [Return Type]
  58. [Postcondition]
  59. ]
  60. [
  61. [`iter_swap(a, b)`]
  62. [`void`]
  63. [the pointed to values are exchanged]
  64. ]
  65. ]
  66. [blurb *Note:* An iterator that is a model of the *Readable* and *Writable Iterator* concepts
  67. is also a model of *Swappable Iterator*. *--end note*]
  68. [endsect]
  69. [section:lvalue Lvalue Iterator Concept]
  70. The *Lvalue Iterator* concept adds the requirement that the return
  71. type of `operator*` type be a reference to the value type of the
  72. iterator.
  73. [table Lvalue Iterator Requirements
  74. [
  75. [Expression]
  76. [Return Type]
  77. [Note/Assertion]
  78. ]
  79. [
  80. [`*a` ]
  81. [`T&` ]
  82. [
  83. `T` is *cv* `iterator_traits<X>::value_type` where *cv* is an optional cv-qualification.
  84. pre: `a` is dereferenceable. If `a == b` then `*a` is equivalent to `*b`.
  85. ]
  86. ]
  87. ]
  88. [endsect]
  89. [endsect]
  90. [section:traversal Traversal]
  91. [section:incrementable Incrementable Iterator Concept]
  92. A class or built-in type `X` models the *Incrementable Iterator*
  93. concept if, in addition to `X` being Assignable and Copy
  94. Constructible, the following expressions are valid and respect the
  95. stated semantics.
  96. [table Incrementable Iterator Requirements (in addition to Assignable, Copy Constructible)
  97. [
  98. [Expression ]
  99. [Return Type]
  100. [Assertion/Semantics ]
  101. ]
  102. [
  103. [`++r` ]
  104. [`X&` ]
  105. [`&r == &++r`]
  106. ]
  107. [
  108. [`r++` ]
  109. [`X` ]
  110. [``
  111. {
  112. X tmp = r;
  113. ++r;
  114. return tmp;
  115. }
  116. ``]
  117. ]
  118. [
  119. [`iterator_traversal<X>::type`]
  120. [Convertible to `incrementable_traversal_tag`]
  121. []
  122. ]
  123. ]
  124. [endsect]
  125. [section:single_pass Single Pass Iterator Concept]
  126. A class or built-in type `X` models the *Single Pass Iterator*
  127. concept if the following expressions are valid and respect the stated
  128. semantics.
  129. [table Single Pass Iterator Requirements (in addition to Incrementable Iterator and Equality Comparable)
  130. [
  131. [Expression]
  132. [Return Type]
  133. [Assertion/Semantics / Pre-/Post-condition]
  134. ]
  135. [
  136. [`++r`]
  137. [`X&`]
  138. [pre:[br]`r` is dereferenceable;[br]post:[br]`r` is dereferenceable or[br]`r` is past-the-end]
  139. ]
  140. [
  141. [`a == b`]
  142. [convertible to `bool`]
  143. [`==` is an equivalence relation over its domain]
  144. ]
  145. [
  146. [`a != b`]
  147. [convertible to `bool`]
  148. [`!(a == b)`]
  149. ]
  150. [
  151. [`iterator_traits<X>::difference_type`]
  152. [A signed integral type representing the distance between iterators]
  153. []
  154. ]
  155. [
  156. [`iterator_traversal<X>::type`]
  157. [Convertible to`single_pass_traversal_tag`]
  158. []
  159. ]
  160. ]
  161. [endsect]
  162. [section:forward Forward Traversal Concept]
  163. A class or built-in type `X` models the *Forward Traversal*
  164. concept if, in addition to `X` meeting the requirements of Default
  165. Constructible and Single Pass Iterator, the following expressions are
  166. valid and respect the stated semantics.
  167. [table Forward Traversal Iterator Requirements (in addition to Default Constructible and Single Pass Iterator)
  168. [
  169. [Expression]
  170. [Return Type]
  171. [Assertion/Note]
  172. ]
  173. [
  174. [`X u;`]
  175. [`X&`]
  176. [note: `u` may have a singular value.]
  177. ]
  178. [
  179. [`++r`]
  180. [`X&`]
  181. [`r == s` and `r` is dereferenceable implies `++r == ++s.`]
  182. ]
  183. [
  184. [`iterator_traversal<X>::type`]
  185. [Convertible to `forward_traversal_tag`]
  186. []
  187. ]
  188. ]
  189. [endsect]
  190. [section:bidirectional Bidirectional Traversal Concept]
  191. A class or built-in type `X` models the *Bidirectional Traversal*
  192. concept if, in addition to `X` meeting the requirements of Forward
  193. Traversal Iterator, the following expressions are valid and respect
  194. the stated semantics.
  195. [table Bidirectional Traversal Iterator Requirements (in addition to Forward Traversal Iterator)
  196. [
  197. [Expression]
  198. [Return Type]
  199. [Assertion/Semantics/Pre-/Post-condition]
  200. ]
  201. [
  202. [`--r`]
  203. [`X&`]
  204. [pre: there exists `s` such that `r == ++s`.[br] post: `s` is dereferenceable. `--(++r) == r`. `--r == --s` implies `r == s`. `&r == &--r`.]
  205. ]
  206. [
  207. [`r--`]
  208. [convertible to `const X&`]
  209. [``
  210. {
  211. X tmp = r;
  212. --r;
  213. return tmp;
  214. }
  215. ``]
  216. ]
  217. [
  218. [`iterator_traversal<X>::type`]
  219. [Convertible to `bidirectional_traversal_tag`]
  220. []
  221. ]
  222. ]
  223. [endsect]
  224. [section:random_access Random Access Traversal Concept]
  225. A class or built-in type `X` models the *Random Access Traversal*
  226. concept if the following expressions are valid and respect the stated
  227. semantics. In the table below, `Distance` is
  228. `iterator_traits<X>::difference_type` and `n` represents a
  229. constant object of type `Distance`.
  230. [table Random Access Traversal Iterator Requirements (in addition to Bidirectional Traversal)
  231. [
  232. [Expression]
  233. [Return Type]
  234. [Operational Semantics]
  235. [Assertion/Precondition]
  236. ]
  237. [
  238. [`r += n`]
  239. [ `X&`]
  240. [``
  241. {
  242. Distance m = n;
  243. if (m >= 0)
  244. while (m--)
  245. ++r;
  246. else
  247. while (m++)
  248. --r;
  249. return r;
  250. }
  251. ``]
  252. [ ]
  253. ]
  254. [
  255. [`a + n`, `n + a`]
  256. [`X`]
  257. [``
  258. {
  259. X tmp = a;
  260. return tmp+= n;
  261. }
  262. ``]
  263. []
  264. ]
  265. [
  266. [`r -= n`]
  267. [`X&`]
  268. [`return r += -n`]
  269. []
  270. ]
  271. [
  272. [`a - n`]
  273. [`X`]
  274. [``
  275. {
  276. X tmp = a;
  277. return tmp-= n;
  278. }
  279. ``]
  280. []
  281. ]
  282. [
  283. [`b - a`]
  284. [`Distance`]
  285. [`a < b ? distance(a,b) : -distance(b,a)`]
  286. [pre: there exists a value `n` of `Distance` such that `a + n == b`. `b == a + (b - a)`.]
  287. ]
  288. [
  289. [`a\[n\]`]
  290. [convertible to T]
  291. [`*(a + n)`]
  292. [pre: a is a *Readable Iterator*]
  293. ]
  294. [
  295. [`a\[n\] = v`]
  296. [convertible to T]
  297. [`*(a + n) = v`]
  298. [pre: a is a *Writable iterator*]
  299. ]
  300. [
  301. [`a < b`]
  302. [convertible to `bool`]
  303. [`b - a > 0`]
  304. [`<` is a total ordering relation]
  305. ]
  306. [
  307. [`a > b`]
  308. [convertible to `bool`]
  309. [`b < a`]
  310. [`>` is a total ordering relation]
  311. ]
  312. [
  313. [`a >= b`]
  314. [convertible to `bool`]
  315. [`!(a < b)`]
  316. []
  317. ]
  318. [
  319. [`a <= b`]
  320. [convertible to `bool`]
  321. [`!(a > b)`]
  322. []
  323. ]
  324. [
  325. [`iterator_traversal<X>::type`]
  326. [convertible to `random_access_traversal_tag`]
  327. []
  328. []
  329. ]
  330. ]
  331. [endsect]
  332. [endsect]
  333. [endsect]