9
3

operators.hpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. // Boost operators.hpp header file ----------------------------------------//
  2. // (C) Copyright David Abrahams, Jeremy Siek, Daryle Walker 1999-2001.
  3. // (C) Copyright Daniel Frey 2002-2017.
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org/libs/utility/operators.htm for documentation.
  8. // Revision History
  9. // 23 Nov 17 Protect dereferenceable<> from overloaded operator&.
  10. // 15 Oct 17 Adapted to C++17, replace std::iterator<> with manual
  11. // implementation.
  12. // 22 Feb 16 Added ADL protection, preserve old work-arounds in
  13. // operators_v1.hpp and clean up this file. (Daniel Frey)
  14. // 16 Dec 10 Limit warning suppression for 4284 to older versions of VC++
  15. // (Matthew Bradbury, fixes #4432)
  16. // 07 Aug 08 Added "euclidean" spelling. (Daniel Frey)
  17. // 03 Apr 08 Make sure "convertible to bool" is sufficient
  18. // for T::operator<, etc. (Daniel Frey)
  19. // 24 May 07 Changed empty_base to depend on T, see
  20. // http://svn.boost.org/trac/boost/ticket/979
  21. // 21 Oct 02 Modified implementation of operators to allow compilers with a
  22. // correct named return value optimization (NRVO) to produce optimal
  23. // code. (Daniel Frey)
  24. // 02 Dec 01 Bug fixed in random_access_iteratable. (Helmut Zeisel)
  25. // 28 Sep 01 Factored out iterator operator groups. (Daryle Walker)
  26. // 27 Aug 01 'left' form for non commutative operators added;
  27. // additional classes for groups of related operators added;
  28. // workaround for empty base class optimization
  29. // bug of GCC 3.0 (Helmut Zeisel)
  30. // 25 Jun 01 output_iterator_helper changes: removed default template
  31. // parameters, added support for self-proxying, additional
  32. // documentation and tests (Aleksey Gurtovoy)
  33. // 29 May 01 Added operator classes for << and >>. Added input and output
  34. // iterator helper classes. Added classes to connect equality and
  35. // relational operators. Added classes for groups of related
  36. // operators. Reimplemented example operator and iterator helper
  37. // classes in terms of the new groups. (Daryle Walker, with help
  38. // from Alexy Gurtovoy)
  39. // 11 Feb 01 Fixed bugs in the iterator helpers which prevented explicitly
  40. // supplied arguments from actually being used (Dave Abrahams)
  41. // 04 Jul 00 Fixed NO_OPERATORS_IN_NAMESPACE bugs, major cleanup and
  42. // refactoring of compiler workarounds, additional documentation
  43. // (Alexy Gurtovoy and Mark Rodgers with some help and prompting from
  44. // Dave Abrahams)
  45. // 28 Jun 00 General cleanup and integration of bugfixes from Mark Rodgers and
  46. // Jeremy Siek (Dave Abrahams)
  47. // 20 Jun 00 Changes to accommodate Borland C++Builder 4 and Borland C++ 5.5
  48. // (Mark Rodgers)
  49. // 20 Jun 00 Minor fixes to the prior revision (Aleksey Gurtovoy)
  50. // 10 Jun 00 Support for the base class chaining technique was added
  51. // (Aleksey Gurtovoy). See documentation and the comments below
  52. // for the details.
  53. // 12 Dec 99 Initial version with iterator operators (Jeremy Siek)
  54. // 18 Nov 99 Change name "divideable" to "dividable", remove unnecessary
  55. // specializations of dividable, subtractable, modable (Ed Brey)
  56. // 17 Nov 99 Add comments (Beman Dawes)
  57. // Remove unnecessary specialization of operators<> (Ed Brey)
  58. // 15 Nov 99 Fix less_than_comparable<T,U> second operand type for first two
  59. // operators.(Beman Dawes)
  60. // 12 Nov 99 Add operators templates (Ed Brey)
  61. // 11 Nov 99 Add single template parameter version for compilers without
  62. // partial specialization (Beman Dawes)
  63. // 10 Nov 99 Initial version
  64. // 10 Jun 00:
  65. // An additional optional template parameter was added to most of
  66. // operator templates to support the base class chaining technique (see
  67. // documentation for the details). Unfortunately, a straightforward
  68. // implementation of this change would have broken compatibility with the
  69. // previous version of the library by making it impossible to use the same
  70. // template name (e.g. 'addable') for both the 1- and 2-argument versions of
  71. // an operator template. This implementation solves the backward-compatibility
  72. // issue at the cost of some simplicity.
  73. //
  74. // One of the complications is an existence of special auxiliary class template
  75. // 'is_chained_base<>' (see 'operators_detail' namespace below), which is used
  76. // to determine whether its template parameter is a library's operator template
  77. // or not. You have to specialize 'is_chained_base<>' for each new
  78. // operator template you add to the library.
  79. //
  80. // However, most of the non-trivial implementation details are hidden behind
  81. // several local macros defined below, and as soon as you understand them,
  82. // you understand the whole library implementation.
  83. #ifndef BOOST_OPERATORS_HPP
  84. #define BOOST_OPERATORS_HPP
  85. // If old work-arounds are needed, refer to the preserved version without
  86. // ADL protection.
  87. #if defined(BOOST_NO_OPERATORS_IN_NAMESPACE) || defined(BOOST_USE_OPERATORS_V1)
  88. #include "operators_v1.hpp"
  89. #else
  90. #include <cstddef>
  91. #include <iterator>
  92. #include <boost/config.hpp>
  93. #include <boost/detail/workaround.hpp>
  94. #include <boost/core/addressof.hpp>
  95. #if defined(__sgi) && !defined(__GNUC__)
  96. # pragma set woff 1234
  97. #endif
  98. #if BOOST_WORKAROUND(BOOST_MSVC, < 1600)
  99. # pragma warning( disable : 4284 ) // complaint about return type of
  100. #endif // operator-> not begin a UDT
  101. // In this section we supply the xxxx1 and xxxx2 forms of the operator
  102. // templates, which are explicitly targeted at the 1-type-argument and
  103. // 2-type-argument operator forms, respectively.
  104. namespace boost
  105. {
  106. namespace operators_impl
  107. {
  108. namespace operators_detail
  109. {
  110. template <typename T> class empty_base {};
  111. } // namespace operators_detail
  112. // Basic operator classes (contributed by Dave Abrahams) ------------------//
  113. // Note that friend functions defined in a class are implicitly inline.
  114. // See the C++ std, 11.4 [class.friend] paragraph 5
  115. template <class T, class U, class B = operators_detail::empty_base<T> >
  116. struct less_than_comparable2 : B
  117. {
  118. friend bool operator<=(const T& x, const U& y) { return !static_cast<bool>(x > y); }
  119. friend bool operator>=(const T& x, const U& y) { return !static_cast<bool>(x < y); }
  120. friend bool operator>(const U& x, const T& y) { return y < x; }
  121. friend bool operator<(const U& x, const T& y) { return y > x; }
  122. friend bool operator<=(const U& x, const T& y) { return !static_cast<bool>(y < x); }
  123. friend bool operator>=(const U& x, const T& y) { return !static_cast<bool>(y > x); }
  124. };
  125. template <class T, class B = operators_detail::empty_base<T> >
  126. struct less_than_comparable1 : B
  127. {
  128. friend bool operator>(const T& x, const T& y) { return y < x; }
  129. friend bool operator<=(const T& x, const T& y) { return !static_cast<bool>(y < x); }
  130. friend bool operator>=(const T& x, const T& y) { return !static_cast<bool>(x < y); }
  131. };
  132. template <class T, class U, class B = operators_detail::empty_base<T> >
  133. struct equality_comparable2 : B
  134. {
  135. friend bool operator==(const U& y, const T& x) { return x == y; }
  136. friend bool operator!=(const U& y, const T& x) { return !static_cast<bool>(x == y); }
  137. friend bool operator!=(const T& y, const U& x) { return !static_cast<bool>(y == x); }
  138. };
  139. template <class T, class B = operators_detail::empty_base<T> >
  140. struct equality_comparable1 : B
  141. {
  142. friend bool operator!=(const T& x, const T& y) { return !static_cast<bool>(x == y); }
  143. };
  144. // A macro which produces "name_2left" from "name".
  145. #define BOOST_OPERATOR2_LEFT(name) name##2##_##left
  146. // NRVO-friendly implementation (contributed by Daniel Frey) ---------------//
  147. #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  148. // This is the optimal implementation for ISO/ANSI C++,
  149. // but it requires the compiler to implement the NRVO.
  150. // If the compiler has no NRVO, this is the best symmetric
  151. // implementation available.
  152. #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \
  153. template <class T, class U, class B = operators_detail::empty_base<T> > \
  154. struct NAME##2 : B \
  155. { \
  156. friend T operator OP( const T& lhs, const U& rhs ) \
  157. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  158. friend T operator OP( const U& lhs, const T& rhs ) \
  159. { T nrv( rhs ); nrv OP##= lhs; return nrv; } \
  160. }; \
  161. \
  162. template <class T, class B = operators_detail::empty_base<T> > \
  163. struct NAME##1 : B \
  164. { \
  165. friend T operator OP( const T& lhs, const T& rhs ) \
  166. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  167. };
  168. #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \
  169. template <class T, class U, class B = operators_detail::empty_base<T> > \
  170. struct NAME##2 : B \
  171. { \
  172. friend T operator OP( const T& lhs, const U& rhs ) \
  173. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  174. }; \
  175. \
  176. template <class T, class U, class B = operators_detail::empty_base<T> > \
  177. struct BOOST_OPERATOR2_LEFT(NAME) : B \
  178. { \
  179. friend T operator OP( const U& lhs, const T& rhs ) \
  180. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  181. }; \
  182. \
  183. template <class T, class B = operators_detail::empty_base<T> > \
  184. struct NAME##1 : B \
  185. { \
  186. friend T operator OP( const T& lhs, const T& rhs ) \
  187. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  188. };
  189. #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  190. // For compilers without NRVO the following code is optimal, but not
  191. // symmetric! Note that the implementation of
  192. // BOOST_OPERATOR2_LEFT(NAME) only looks cool, but doesn't provide
  193. // optimization opportunities to the compiler :)
  194. #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \
  195. template <class T, class U, class B = operators_detail::empty_base<T> > \
  196. struct NAME##2 : B \
  197. { \
  198. friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
  199. friend T operator OP( const U& lhs, T rhs ) { return rhs OP##= lhs; } \
  200. }; \
  201. \
  202. template <class T, class B = operators_detail::empty_base<T> > \
  203. struct NAME##1 : B \
  204. { \
  205. friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
  206. };
  207. #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \
  208. template <class T, class U, class B = operators_detail::empty_base<T> > \
  209. struct NAME##2 : B \
  210. { \
  211. friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
  212. }; \
  213. \
  214. template <class T, class U, class B = operators_detail::empty_base<T> > \
  215. struct BOOST_OPERATOR2_LEFT(NAME) : B \
  216. { \
  217. friend T operator OP( const U& lhs, const T& rhs ) \
  218. { return T( lhs ) OP##= rhs; } \
  219. }; \
  220. \
  221. template <class T, class B = operators_detail::empty_base<T> > \
  222. struct NAME##1 : B \
  223. { \
  224. friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
  225. };
  226. #endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  227. BOOST_BINARY_OPERATOR_COMMUTATIVE( multipliable, * )
  228. BOOST_BINARY_OPERATOR_COMMUTATIVE( addable, + )
  229. BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( subtractable, - )
  230. BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( dividable, / )
  231. BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( modable, % )
  232. BOOST_BINARY_OPERATOR_COMMUTATIVE( xorable, ^ )
  233. BOOST_BINARY_OPERATOR_COMMUTATIVE( andable, & )
  234. BOOST_BINARY_OPERATOR_COMMUTATIVE( orable, | )
  235. #undef BOOST_BINARY_OPERATOR_COMMUTATIVE
  236. #undef BOOST_BINARY_OPERATOR_NON_COMMUTATIVE
  237. #undef BOOST_OPERATOR2_LEFT
  238. // incrementable and decrementable contributed by Jeremy Siek
  239. template <class T, class B = operators_detail::empty_base<T> >
  240. struct incrementable : B
  241. {
  242. friend T operator++(T& x, int)
  243. {
  244. incrementable_type nrv(x);
  245. ++x;
  246. return nrv;
  247. }
  248. private: // The use of this typedef works around a Borland bug
  249. typedef T incrementable_type;
  250. };
  251. template <class T, class B = operators_detail::empty_base<T> >
  252. struct decrementable : B
  253. {
  254. friend T operator--(T& x, int)
  255. {
  256. decrementable_type nrv(x);
  257. --x;
  258. return nrv;
  259. }
  260. private: // The use of this typedef works around a Borland bug
  261. typedef T decrementable_type;
  262. };
  263. // Iterator operator classes (contributed by Jeremy Siek) ------------------//
  264. template <class T, class P, class B = operators_detail::empty_base<T> >
  265. struct dereferenceable : B
  266. {
  267. P operator->() const
  268. {
  269. return ::boost::addressof(*static_cast<const T&>(*this));
  270. }
  271. };
  272. template <class T, class I, class R, class B = operators_detail::empty_base<T> >
  273. struct indexable : B
  274. {
  275. R operator[](I n) const
  276. {
  277. return *(static_cast<const T&>(*this) + n);
  278. }
  279. };
  280. // More operator classes (contributed by Daryle Walker) --------------------//
  281. // (NRVO-friendly implementation contributed by Daniel Frey) ---------------//
  282. #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  283. #define BOOST_BINARY_OPERATOR( NAME, OP ) \
  284. template <class T, class U, class B = operators_detail::empty_base<T> > \
  285. struct NAME##2 : B \
  286. { \
  287. friend T operator OP( const T& lhs, const U& rhs ) \
  288. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  289. }; \
  290. \
  291. template <class T, class B = operators_detail::empty_base<T> > \
  292. struct NAME##1 : B \
  293. { \
  294. friend T operator OP( const T& lhs, const T& rhs ) \
  295. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  296. };
  297. #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  298. #define BOOST_BINARY_OPERATOR( NAME, OP ) \
  299. template <class T, class U, class B = operators_detail::empty_base<T> > \
  300. struct NAME##2 : B \
  301. { \
  302. friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
  303. }; \
  304. \
  305. template <class T, class B = operators_detail::empty_base<T> > \
  306. struct NAME##1 : B \
  307. { \
  308. friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
  309. };
  310. #endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  311. BOOST_BINARY_OPERATOR( left_shiftable, << )
  312. BOOST_BINARY_OPERATOR( right_shiftable, >> )
  313. #undef BOOST_BINARY_OPERATOR
  314. template <class T, class U, class B = operators_detail::empty_base<T> >
  315. struct equivalent2 : B
  316. {
  317. friend bool operator==(const T& x, const U& y)
  318. {
  319. return !static_cast<bool>(x < y) && !static_cast<bool>(x > y);
  320. }
  321. };
  322. template <class T, class B = operators_detail::empty_base<T> >
  323. struct equivalent1 : B
  324. {
  325. friend bool operator==(const T&x, const T&y)
  326. {
  327. return !static_cast<bool>(x < y) && !static_cast<bool>(y < x);
  328. }
  329. };
  330. template <class T, class U, class B = operators_detail::empty_base<T> >
  331. struct partially_ordered2 : B
  332. {
  333. friend bool operator<=(const T& x, const U& y)
  334. { return static_cast<bool>(x < y) || static_cast<bool>(x == y); }
  335. friend bool operator>=(const T& x, const U& y)
  336. { return static_cast<bool>(x > y) || static_cast<bool>(x == y); }
  337. friend bool operator>(const U& x, const T& y)
  338. { return y < x; }
  339. friend bool operator<(const U& x, const T& y)
  340. { return y > x; }
  341. friend bool operator<=(const U& x, const T& y)
  342. { return static_cast<bool>(y > x) || static_cast<bool>(y == x); }
  343. friend bool operator>=(const U& x, const T& y)
  344. { return static_cast<bool>(y < x) || static_cast<bool>(y == x); }
  345. };
  346. template <class T, class B = operators_detail::empty_base<T> >
  347. struct partially_ordered1 : B
  348. {
  349. friend bool operator>(const T& x, const T& y)
  350. { return y < x; }
  351. friend bool operator<=(const T& x, const T& y)
  352. { return static_cast<bool>(x < y) || static_cast<bool>(x == y); }
  353. friend bool operator>=(const T& x, const T& y)
  354. { return static_cast<bool>(y < x) || static_cast<bool>(x == y); }
  355. };
  356. // Combined operator classes (contributed by Daryle Walker) ----------------//
  357. template <class T, class U, class B = operators_detail::empty_base<T> >
  358. struct totally_ordered2
  359. : less_than_comparable2<T, U
  360. , equality_comparable2<T, U, B
  361. > > {};
  362. template <class T, class B = operators_detail::empty_base<T> >
  363. struct totally_ordered1
  364. : less_than_comparable1<T
  365. , equality_comparable1<T, B
  366. > > {};
  367. template <class T, class U, class B = operators_detail::empty_base<T> >
  368. struct additive2
  369. : addable2<T, U
  370. , subtractable2<T, U, B
  371. > > {};
  372. template <class T, class B = operators_detail::empty_base<T> >
  373. struct additive1
  374. : addable1<T
  375. , subtractable1<T, B
  376. > > {};
  377. template <class T, class U, class B = operators_detail::empty_base<T> >
  378. struct multiplicative2
  379. : multipliable2<T, U
  380. , dividable2<T, U, B
  381. > > {};
  382. template <class T, class B = operators_detail::empty_base<T> >
  383. struct multiplicative1
  384. : multipliable1<T
  385. , dividable1<T, B
  386. > > {};
  387. template <class T, class U, class B = operators_detail::empty_base<T> >
  388. struct integer_multiplicative2
  389. : multiplicative2<T, U
  390. , modable2<T, U, B
  391. > > {};
  392. template <class T, class B = operators_detail::empty_base<T> >
  393. struct integer_multiplicative1
  394. : multiplicative1<T
  395. , modable1<T, B
  396. > > {};
  397. template <class T, class U, class B = operators_detail::empty_base<T> >
  398. struct arithmetic2
  399. : additive2<T, U
  400. , multiplicative2<T, U, B
  401. > > {};
  402. template <class T, class B = operators_detail::empty_base<T> >
  403. struct arithmetic1
  404. : additive1<T
  405. , multiplicative1<T, B
  406. > > {};
  407. template <class T, class U, class B = operators_detail::empty_base<T> >
  408. struct integer_arithmetic2
  409. : additive2<T, U
  410. , integer_multiplicative2<T, U, B
  411. > > {};
  412. template <class T, class B = operators_detail::empty_base<T> >
  413. struct integer_arithmetic1
  414. : additive1<T
  415. , integer_multiplicative1<T, B
  416. > > {};
  417. template <class T, class U, class B = operators_detail::empty_base<T> >
  418. struct bitwise2
  419. : xorable2<T, U
  420. , andable2<T, U
  421. , orable2<T, U, B
  422. > > > {};
  423. template <class T, class B = operators_detail::empty_base<T> >
  424. struct bitwise1
  425. : xorable1<T
  426. , andable1<T
  427. , orable1<T, B
  428. > > > {};
  429. template <class T, class B = operators_detail::empty_base<T> >
  430. struct unit_steppable
  431. : incrementable<T
  432. , decrementable<T, B
  433. > > {};
  434. template <class T, class U, class B = operators_detail::empty_base<T> >
  435. struct shiftable2
  436. : left_shiftable2<T, U
  437. , right_shiftable2<T, U, B
  438. > > {};
  439. template <class T, class B = operators_detail::empty_base<T> >
  440. struct shiftable1
  441. : left_shiftable1<T
  442. , right_shiftable1<T, B
  443. > > {};
  444. template <class T, class U, class B = operators_detail::empty_base<T> >
  445. struct ring_operators2
  446. : additive2<T, U
  447. , subtractable2_left<T, U
  448. , multipliable2<T, U, B
  449. > > > {};
  450. template <class T, class B = operators_detail::empty_base<T> >
  451. struct ring_operators1
  452. : additive1<T
  453. , multipliable1<T, B
  454. > > {};
  455. template <class T, class U, class B = operators_detail::empty_base<T> >
  456. struct ordered_ring_operators2
  457. : ring_operators2<T, U
  458. , totally_ordered2<T, U, B
  459. > > {};
  460. template <class T, class B = operators_detail::empty_base<T> >
  461. struct ordered_ring_operators1
  462. : ring_operators1<T
  463. , totally_ordered1<T, B
  464. > > {};
  465. template <class T, class U, class B = operators_detail::empty_base<T> >
  466. struct field_operators2
  467. : ring_operators2<T, U
  468. , dividable2<T, U
  469. , dividable2_left<T, U, B
  470. > > > {};
  471. template <class T, class B = operators_detail::empty_base<T> >
  472. struct field_operators1
  473. : ring_operators1<T
  474. , dividable1<T, B
  475. > > {};
  476. template <class T, class U, class B = operators_detail::empty_base<T> >
  477. struct ordered_field_operators2
  478. : field_operators2<T, U
  479. , totally_ordered2<T, U, B
  480. > > {};
  481. template <class T, class B = operators_detail::empty_base<T> >
  482. struct ordered_field_operators1
  483. : field_operators1<T
  484. , totally_ordered1<T, B
  485. > > {};
  486. template <class T, class U, class B = operators_detail::empty_base<T> >
  487. struct euclidian_ring_operators2
  488. : ring_operators2<T, U
  489. , dividable2<T, U
  490. , dividable2_left<T, U
  491. , modable2<T, U
  492. , modable2_left<T, U, B
  493. > > > > > {};
  494. template <class T, class B = operators_detail::empty_base<T> >
  495. struct euclidian_ring_operators1
  496. : ring_operators1<T
  497. , dividable1<T
  498. , modable1<T, B
  499. > > > {};
  500. template <class T, class U, class B = operators_detail::empty_base<T> >
  501. struct ordered_euclidian_ring_operators2
  502. : totally_ordered2<T, U
  503. , euclidian_ring_operators2<T, U, B
  504. > > {};
  505. template <class T, class B = operators_detail::empty_base<T> >
  506. struct ordered_euclidian_ring_operators1
  507. : totally_ordered1<T
  508. , euclidian_ring_operators1<T, B
  509. > > {};
  510. template <class T, class U, class B = operators_detail::empty_base<T> >
  511. struct euclidean_ring_operators2
  512. : ring_operators2<T, U
  513. , dividable2<T, U
  514. , dividable2_left<T, U
  515. , modable2<T, U
  516. , modable2_left<T, U, B
  517. > > > > > {};
  518. template <class T, class B = operators_detail::empty_base<T> >
  519. struct euclidean_ring_operators1
  520. : ring_operators1<T
  521. , dividable1<T
  522. , modable1<T, B
  523. > > > {};
  524. template <class T, class U, class B = operators_detail::empty_base<T> >
  525. struct ordered_euclidean_ring_operators2
  526. : totally_ordered2<T, U
  527. , euclidean_ring_operators2<T, U, B
  528. > > {};
  529. template <class T, class B = operators_detail::empty_base<T> >
  530. struct ordered_euclidean_ring_operators1
  531. : totally_ordered1<T
  532. , euclidean_ring_operators1<T, B
  533. > > {};
  534. template <class T, class P, class B = operators_detail::empty_base<T> >
  535. struct input_iteratable
  536. : equality_comparable1<T
  537. , incrementable<T
  538. , dereferenceable<T, P, B
  539. > > > {};
  540. template <class T, class B = operators_detail::empty_base<T> >
  541. struct output_iteratable
  542. : incrementable<T, B
  543. > {};
  544. template <class T, class P, class B = operators_detail::empty_base<T> >
  545. struct forward_iteratable
  546. : input_iteratable<T, P, B
  547. > {};
  548. template <class T, class P, class B = operators_detail::empty_base<T> >
  549. struct bidirectional_iteratable
  550. : forward_iteratable<T, P
  551. , decrementable<T, B
  552. > > {};
  553. // To avoid repeated derivation from equality_comparable,
  554. // which is an indirect base class of bidirectional_iterable,
  555. // random_access_iteratable must not be derived from totally_ordered1
  556. // but from less_than_comparable1 only. (Helmut Zeisel, 02-Dec-2001)
  557. template <class T, class P, class D, class R, class B = operators_detail::empty_base<T> >
  558. struct random_access_iteratable
  559. : bidirectional_iteratable<T, P
  560. , less_than_comparable1<T
  561. , additive2<T, D
  562. , indexable<T, D, R, B
  563. > > > > {};
  564. //
  565. // Here's where we put it all together, defining the xxxx forms of the templates.
  566. // We also define specializations of is_chained_base<> for
  567. // the xxxx, xxxx1, and xxxx2 templates.
  568. //
  569. namespace operators_detail
  570. {
  571. // A type parameter is used instead of a plain bool because Borland's compiler
  572. // didn't cope well with the more obvious non-type template parameter.
  573. struct true_t {};
  574. struct false_t {};
  575. } // namespace operators_detail
  576. // is_chained_base<> - a traits class used to distinguish whether an operator
  577. // template argument is being used for base class chaining, or is specifying a
  578. // 2nd argument type.
  579. // Unspecialized version assumes that most types are not being used for base
  580. // class chaining. We specialize for the operator templates defined in this
  581. // library.
  582. template<class T> struct is_chained_base {
  583. typedef operators_detail::false_t value;
  584. };
  585. // Provide a specialization of 'is_chained_base<>'
  586. // for a 4-type-argument operator template.
  587. # define BOOST_OPERATOR_TEMPLATE4(template_name4) \
  588. template<class T, class U, class V, class W, class B> \
  589. struct is_chained_base< template_name4<T, U, V, W, B> > { \
  590. typedef operators_detail::true_t value; \
  591. };
  592. // Provide a specialization of 'is_chained_base<>'
  593. // for a 3-type-argument operator template.
  594. # define BOOST_OPERATOR_TEMPLATE3(template_name3) \
  595. template<class T, class U, class V, class B> \
  596. struct is_chained_base< template_name3<T, U, V, B> > { \
  597. typedef operators_detail::true_t value; \
  598. };
  599. // Provide a specialization of 'is_chained_base<>'
  600. // for a 2-type-argument operator template.
  601. # define BOOST_OPERATOR_TEMPLATE2(template_name2) \
  602. template<class T, class U, class B> \
  603. struct is_chained_base< template_name2<T, U, B> > { \
  604. typedef operators_detail::true_t value; \
  605. };
  606. // Provide a specialization of 'is_chained_base<>'
  607. // for a 1-type-argument operator template.
  608. # define BOOST_OPERATOR_TEMPLATE1(template_name1) \
  609. template<class T, class B> \
  610. struct is_chained_base< template_name1<T, B> > { \
  611. typedef operators_detail::true_t value; \
  612. };
  613. // BOOST_OPERATOR_TEMPLATE(template_name) defines template_name<> such that it
  614. // can be used for specifying both 1-argument and 2-argument forms. Requires the
  615. // existence of two previously defined class templates named '<template_name>1'
  616. // and '<template_name>2' which must implement the corresponding 1- and 2-
  617. // argument forms.
  618. //
  619. // The template type parameter O == is_chained_base<U>::value is used to
  620. // distinguish whether the 2nd argument to <template_name> is being used for
  621. // base class chaining from another boost operator template or is describing a
  622. // 2nd operand type. O == true_t only when U is actually an another operator
  623. // template from the library. Partial specialization is used to select an
  624. // implementation in terms of either '<template_name>1' or '<template_name>2'.
  625. //
  626. # define BOOST_OPERATOR_TEMPLATE(template_name) \
  627. template <class T \
  628. ,class U = T \
  629. ,class B = operators_detail::empty_base<T> \
  630. ,class O = typename is_chained_base<U>::value \
  631. > \
  632. struct template_name; \
  633. \
  634. template<class T, class U, class B> \
  635. struct template_name<T, U, B, operators_detail::false_t> \
  636. : template_name##2<T, U, B> {}; \
  637. \
  638. template<class T, class U> \
  639. struct template_name<T, U, operators_detail::empty_base<T>, operators_detail::true_t> \
  640. : template_name##1<T, U> {}; \
  641. \
  642. template <class T, class B> \
  643. struct template_name<T, T, B, operators_detail::false_t> \
  644. : template_name##1<T, B> {}; \
  645. \
  646. template<class T, class U, class B, class O> \
  647. struct is_chained_base< template_name<T, U, B, O> > { \
  648. typedef operators_detail::true_t value; \
  649. }; \
  650. \
  651. BOOST_OPERATOR_TEMPLATE2(template_name##2) \
  652. BOOST_OPERATOR_TEMPLATE1(template_name##1)
  653. BOOST_OPERATOR_TEMPLATE(less_than_comparable)
  654. BOOST_OPERATOR_TEMPLATE(equality_comparable)
  655. BOOST_OPERATOR_TEMPLATE(multipliable)
  656. BOOST_OPERATOR_TEMPLATE(addable)
  657. BOOST_OPERATOR_TEMPLATE(subtractable)
  658. BOOST_OPERATOR_TEMPLATE2(subtractable2_left)
  659. BOOST_OPERATOR_TEMPLATE(dividable)
  660. BOOST_OPERATOR_TEMPLATE2(dividable2_left)
  661. BOOST_OPERATOR_TEMPLATE(modable)
  662. BOOST_OPERATOR_TEMPLATE2(modable2_left)
  663. BOOST_OPERATOR_TEMPLATE(xorable)
  664. BOOST_OPERATOR_TEMPLATE(andable)
  665. BOOST_OPERATOR_TEMPLATE(orable)
  666. BOOST_OPERATOR_TEMPLATE1(incrementable)
  667. BOOST_OPERATOR_TEMPLATE1(decrementable)
  668. BOOST_OPERATOR_TEMPLATE2(dereferenceable)
  669. BOOST_OPERATOR_TEMPLATE3(indexable)
  670. BOOST_OPERATOR_TEMPLATE(left_shiftable)
  671. BOOST_OPERATOR_TEMPLATE(right_shiftable)
  672. BOOST_OPERATOR_TEMPLATE(equivalent)
  673. BOOST_OPERATOR_TEMPLATE(partially_ordered)
  674. BOOST_OPERATOR_TEMPLATE(totally_ordered)
  675. BOOST_OPERATOR_TEMPLATE(additive)
  676. BOOST_OPERATOR_TEMPLATE(multiplicative)
  677. BOOST_OPERATOR_TEMPLATE(integer_multiplicative)
  678. BOOST_OPERATOR_TEMPLATE(arithmetic)
  679. BOOST_OPERATOR_TEMPLATE(integer_arithmetic)
  680. BOOST_OPERATOR_TEMPLATE(bitwise)
  681. BOOST_OPERATOR_TEMPLATE1(unit_steppable)
  682. BOOST_OPERATOR_TEMPLATE(shiftable)
  683. BOOST_OPERATOR_TEMPLATE(ring_operators)
  684. BOOST_OPERATOR_TEMPLATE(ordered_ring_operators)
  685. BOOST_OPERATOR_TEMPLATE(field_operators)
  686. BOOST_OPERATOR_TEMPLATE(ordered_field_operators)
  687. BOOST_OPERATOR_TEMPLATE(euclidian_ring_operators)
  688. BOOST_OPERATOR_TEMPLATE(ordered_euclidian_ring_operators)
  689. BOOST_OPERATOR_TEMPLATE(euclidean_ring_operators)
  690. BOOST_OPERATOR_TEMPLATE(ordered_euclidean_ring_operators)
  691. BOOST_OPERATOR_TEMPLATE2(input_iteratable)
  692. BOOST_OPERATOR_TEMPLATE1(output_iteratable)
  693. BOOST_OPERATOR_TEMPLATE2(forward_iteratable)
  694. BOOST_OPERATOR_TEMPLATE2(bidirectional_iteratable)
  695. BOOST_OPERATOR_TEMPLATE4(random_access_iteratable)
  696. #undef BOOST_OPERATOR_TEMPLATE
  697. #undef BOOST_OPERATOR_TEMPLATE4
  698. #undef BOOST_OPERATOR_TEMPLATE3
  699. #undef BOOST_OPERATOR_TEMPLATE2
  700. #undef BOOST_OPERATOR_TEMPLATE1
  701. template <class T, class U>
  702. struct operators2
  703. : totally_ordered2<T,U
  704. , integer_arithmetic2<T,U
  705. , bitwise2<T,U
  706. > > > {};
  707. template <class T, class U = T>
  708. struct operators : operators2<T, U> {};
  709. template <class T> struct operators<T, T>
  710. : totally_ordered<T
  711. , integer_arithmetic<T
  712. , bitwise<T
  713. , unit_steppable<T
  714. > > > > {};
  715. // Iterator helper classes (contributed by Jeremy Siek) -------------------//
  716. // (Input and output iterator helpers contributed by Daryle Walker) -------//
  717. // (Changed to use combined operator classes by Daryle Walker) ------------//
  718. // (Adapted to C++17 by Daniel Frey) --------------------------------------//
  719. template <class Category,
  720. class T,
  721. class Distance = std::ptrdiff_t,
  722. class Pointer = T*,
  723. class Reference = T&>
  724. struct iterator_helper
  725. {
  726. typedef Category iterator_category;
  727. typedef T value_type;
  728. typedef Distance difference_type;
  729. typedef Pointer pointer;
  730. typedef Reference reference;
  731. };
  732. template <class T,
  733. class V,
  734. class D = std::ptrdiff_t,
  735. class P = V const *,
  736. class R = V const &>
  737. struct input_iterator_helper
  738. : input_iteratable<T, P
  739. , iterator_helper<std::input_iterator_tag, V, D, P, R
  740. > > {};
  741. template<class T>
  742. struct output_iterator_helper
  743. : output_iteratable<T
  744. , iterator_helper<std::output_iterator_tag, void, void, void, void
  745. > >
  746. {
  747. T& operator*() { return static_cast<T&>(*this); }
  748. T& operator++() { return static_cast<T&>(*this); }
  749. };
  750. template <class T,
  751. class V,
  752. class D = std::ptrdiff_t,
  753. class P = V*,
  754. class R = V&>
  755. struct forward_iterator_helper
  756. : forward_iteratable<T, P
  757. , iterator_helper<std::forward_iterator_tag, V, D, P, R
  758. > > {};
  759. template <class T,
  760. class V,
  761. class D = std::ptrdiff_t,
  762. class P = V*,
  763. class R = V&>
  764. struct bidirectional_iterator_helper
  765. : bidirectional_iteratable<T, P
  766. , iterator_helper<std::bidirectional_iterator_tag, V, D, P, R
  767. > > {};
  768. template <class T,
  769. class V,
  770. class D = std::ptrdiff_t,
  771. class P = V*,
  772. class R = V&>
  773. struct random_access_iterator_helper
  774. : random_access_iteratable<T, P, D, R
  775. , iterator_helper<std::random_access_iterator_tag, V, D, P, R
  776. > >
  777. {
  778. friend D requires_difference_operator(const T& x, const T& y) {
  779. return x - y;
  780. }
  781. }; // random_access_iterator_helper
  782. } // namespace operators_impl
  783. using namespace operators_impl;
  784. } // namespace boost
  785. #if defined(__sgi) && !defined(__GNUC__)
  786. #pragma reset woff 1234
  787. #endif
  788. #endif // BOOST_NO_OPERATORS_IN_NAMESPACE
  789. #endif // BOOST_OPERATORS_HPP