operators.hpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. // Define BOOST_OPERATORS_CONSTEXPR to be like BOOST_CONSTEXPR but empty under MSVC < v19.22
  102. #if BOOST_WORKAROUND(BOOST_MSVC, < 1922)
  103. #define BOOST_OPERATORS_CONSTEXPR
  104. #else
  105. #define BOOST_OPERATORS_CONSTEXPR BOOST_CONSTEXPR
  106. #endif
  107. // In this section we supply the xxxx1 and xxxx2 forms of the operator
  108. // templates, which are explicitly targeted at the 1-type-argument and
  109. // 2-type-argument operator forms, respectively.
  110. namespace boost
  111. {
  112. namespace operators_impl
  113. {
  114. namespace operators_detail
  115. {
  116. template <typename T> class empty_base {};
  117. } // namespace operators_detail
  118. // Basic operator classes (contributed by Dave Abrahams) ------------------//
  119. // Note that friend functions defined in a class are implicitly inline.
  120. // See the C++ std, 11.4 [class.friend] paragraph 5
  121. template <class T, class U, class B = operators_detail::empty_base<T> >
  122. struct less_than_comparable2 : B
  123. {
  124. friend BOOST_OPERATORS_CONSTEXPR bool operator<=(const T& x, const U& y) { return !static_cast<bool>(x > y); }
  125. friend BOOST_OPERATORS_CONSTEXPR bool operator>=(const T& x, const U& y) { return !static_cast<bool>(x < y); }
  126. friend BOOST_OPERATORS_CONSTEXPR bool operator>(const U& x, const T& y) { return y < x; }
  127. friend BOOST_OPERATORS_CONSTEXPR bool operator<(const U& x, const T& y) { return y > x; }
  128. friend BOOST_OPERATORS_CONSTEXPR bool operator<=(const U& x, const T& y) { return !static_cast<bool>(y < x); }
  129. friend BOOST_OPERATORS_CONSTEXPR bool operator>=(const U& x, const T& y) { return !static_cast<bool>(y > x); }
  130. };
  131. template <class T, class B = operators_detail::empty_base<T> >
  132. struct less_than_comparable1 : B
  133. {
  134. friend BOOST_OPERATORS_CONSTEXPR bool operator>(const T& x, const T& y) { return y < x; }
  135. friend BOOST_OPERATORS_CONSTEXPR bool operator<=(const T& x, const T& y) { return !static_cast<bool>(y < x); }
  136. friend BOOST_OPERATORS_CONSTEXPR bool operator>=(const T& x, const T& y) { return !static_cast<bool>(x < y); }
  137. };
  138. template <class T, class U, class B = operators_detail::empty_base<T> >
  139. struct equality_comparable2 : B
  140. {
  141. friend BOOST_OPERATORS_CONSTEXPR bool operator==(const U& y, const T& x) { return x == y; }
  142. friend BOOST_OPERATORS_CONSTEXPR bool operator!=(const U& y, const T& x) { return !static_cast<bool>(x == y); }
  143. friend BOOST_OPERATORS_CONSTEXPR bool operator!=(const T& y, const U& x) { return !static_cast<bool>(y == x); }
  144. };
  145. template <class T, class B = operators_detail::empty_base<T> >
  146. struct equality_comparable1 : B
  147. {
  148. friend BOOST_OPERATORS_CONSTEXPR bool operator!=(const T& x, const T& y) { return !static_cast<bool>(x == y); }
  149. };
  150. // A macro which produces "name_2left" from "name".
  151. #define BOOST_OPERATOR2_LEFT(name) name##2##_##left
  152. // NRVO-friendly implementation (contributed by Daniel Frey) ---------------//
  153. #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  154. // This is the optimal implementation for ISO/ANSI C++,
  155. // but it requires the compiler to implement the NRVO.
  156. // If the compiler has no NRVO, this is the best symmetric
  157. // implementation available.
  158. #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \
  159. template <class T, class U, class B = operators_detail::empty_base<T> > \
  160. struct NAME##2 : B \
  161. { \
  162. friend T operator OP( const T& lhs, const U& rhs ) \
  163. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  164. friend T operator OP( const U& lhs, const T& rhs ) \
  165. { T nrv( rhs ); nrv OP##= lhs; return nrv; } \
  166. }; \
  167. \
  168. template <class T, class B = operators_detail::empty_base<T> > \
  169. struct NAME##1 : B \
  170. { \
  171. friend T operator OP( const T& lhs, const T& rhs ) \
  172. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  173. };
  174. #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \
  175. template <class T, class U, class B = operators_detail::empty_base<T> > \
  176. struct NAME##2 : B \
  177. { \
  178. friend T operator OP( const T& lhs, const U& rhs ) \
  179. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  180. }; \
  181. \
  182. template <class T, class U, class B = operators_detail::empty_base<T> > \
  183. struct BOOST_OPERATOR2_LEFT(NAME) : B \
  184. { \
  185. friend T operator OP( const U& lhs, const T& rhs ) \
  186. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  187. }; \
  188. \
  189. template <class T, class B = operators_detail::empty_base<T> > \
  190. struct NAME##1 : B \
  191. { \
  192. friend T operator OP( const T& lhs, const T& rhs ) \
  193. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  194. };
  195. #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  196. // For compilers without NRVO the following code is optimal, but not
  197. // symmetric! Note that the implementation of
  198. // BOOST_OPERATOR2_LEFT(NAME) only looks cool, but doesn't provide
  199. // optimization opportunities to the compiler :)
  200. #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \
  201. template <class T, class U, class B = operators_detail::empty_base<T> > \
  202. struct NAME##2 : B \
  203. { \
  204. friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
  205. friend T operator OP( const U& lhs, T rhs ) { return rhs OP##= lhs; } \
  206. }; \
  207. \
  208. template <class T, class B = operators_detail::empty_base<T> > \
  209. struct NAME##1 : B \
  210. { \
  211. friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
  212. };
  213. #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \
  214. template <class T, class U, class B = operators_detail::empty_base<T> > \
  215. struct NAME##2 : B \
  216. { \
  217. friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
  218. }; \
  219. \
  220. template <class T, class U, class B = operators_detail::empty_base<T> > \
  221. struct BOOST_OPERATOR2_LEFT(NAME) : B \
  222. { \
  223. friend T operator OP( const U& lhs, const T& rhs ) \
  224. { return T( lhs ) OP##= rhs; } \
  225. }; \
  226. \
  227. template <class T, class B = operators_detail::empty_base<T> > \
  228. struct NAME##1 : B \
  229. { \
  230. friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
  231. };
  232. #endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  233. BOOST_BINARY_OPERATOR_COMMUTATIVE( multipliable, * )
  234. BOOST_BINARY_OPERATOR_COMMUTATIVE( addable, + )
  235. BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( subtractable, - )
  236. BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( dividable, / )
  237. BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( modable, % )
  238. BOOST_BINARY_OPERATOR_COMMUTATIVE( xorable, ^ )
  239. BOOST_BINARY_OPERATOR_COMMUTATIVE( andable, & )
  240. BOOST_BINARY_OPERATOR_COMMUTATIVE( orable, | )
  241. #undef BOOST_BINARY_OPERATOR_COMMUTATIVE
  242. #undef BOOST_BINARY_OPERATOR_NON_COMMUTATIVE
  243. #undef BOOST_OPERATOR2_LEFT
  244. // incrementable and decrementable contributed by Jeremy Siek
  245. template <class T, class B = operators_detail::empty_base<T> >
  246. struct incrementable : B
  247. {
  248. friend T operator++(T& x, int)
  249. {
  250. incrementable_type nrv(x);
  251. ++x;
  252. return nrv;
  253. }
  254. private: // The use of this typedef works around a Borland bug
  255. typedef T incrementable_type;
  256. };
  257. template <class T, class B = operators_detail::empty_base<T> >
  258. struct decrementable : B
  259. {
  260. friend T operator--(T& x, int)
  261. {
  262. decrementable_type nrv(x);
  263. --x;
  264. return nrv;
  265. }
  266. private: // The use of this typedef works around a Borland bug
  267. typedef T decrementable_type;
  268. };
  269. // Iterator operator classes (contributed by Jeremy Siek) ------------------//
  270. template <class T, class P, class B = operators_detail::empty_base<T> >
  271. struct dereferenceable : B
  272. {
  273. P operator->() const
  274. {
  275. return ::boost::addressof(*static_cast<const T&>(*this));
  276. }
  277. };
  278. template <class T, class I, class R, class B = operators_detail::empty_base<T> >
  279. struct indexable : B
  280. {
  281. R operator[](I n) const
  282. {
  283. return *(static_cast<const T&>(*this) + n);
  284. }
  285. };
  286. // More operator classes (contributed by Daryle Walker) --------------------//
  287. // (NRVO-friendly implementation contributed by Daniel Frey) ---------------//
  288. #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  289. #define BOOST_BINARY_OPERATOR( NAME, OP ) \
  290. template <class T, class U, class B = operators_detail::empty_base<T> > \
  291. struct NAME##2 : B \
  292. { \
  293. friend T operator OP( const T& lhs, const U& rhs ) \
  294. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  295. }; \
  296. \
  297. template <class T, class B = operators_detail::empty_base<T> > \
  298. struct NAME##1 : B \
  299. { \
  300. friend T operator OP( const T& lhs, const T& rhs ) \
  301. { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
  302. };
  303. #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  304. #define BOOST_BINARY_OPERATOR( NAME, OP ) \
  305. template <class T, class U, class B = operators_detail::empty_base<T> > \
  306. struct NAME##2 : B \
  307. { \
  308. friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
  309. }; \
  310. \
  311. template <class T, class B = operators_detail::empty_base<T> > \
  312. struct NAME##1 : B \
  313. { \
  314. friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
  315. };
  316. #endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
  317. BOOST_BINARY_OPERATOR( left_shiftable, << )
  318. BOOST_BINARY_OPERATOR( right_shiftable, >> )
  319. #undef BOOST_BINARY_OPERATOR
  320. template <class T, class U, class B = operators_detail::empty_base<T> >
  321. struct equivalent2 : B
  322. {
  323. friend BOOST_OPERATORS_CONSTEXPR bool operator==(const T& x, const U& y)
  324. {
  325. return !static_cast<bool>(x < y) && !static_cast<bool>(x > y);
  326. }
  327. };
  328. template <class T, class B = operators_detail::empty_base<T> >
  329. struct equivalent1 : B
  330. {
  331. friend BOOST_OPERATORS_CONSTEXPR bool operator==(const T&x, const T&y)
  332. {
  333. return !static_cast<bool>(x < y) && !static_cast<bool>(y < x);
  334. }
  335. };
  336. template <class T, class U, class B = operators_detail::empty_base<T> >
  337. struct partially_ordered2 : B
  338. {
  339. friend BOOST_OPERATORS_CONSTEXPR bool operator<=(const T& x, const U& y)
  340. { return static_cast<bool>(x < y) || static_cast<bool>(x == y); }
  341. friend BOOST_OPERATORS_CONSTEXPR bool operator>=(const T& x, const U& y)
  342. { return static_cast<bool>(x > y) || static_cast<bool>(x == y); }
  343. friend BOOST_OPERATORS_CONSTEXPR bool operator>(const U& x, const T& y)
  344. { return y < x; }
  345. friend BOOST_OPERATORS_CONSTEXPR bool operator<(const U& x, const T& y)
  346. { return y > x; }
  347. friend BOOST_OPERATORS_CONSTEXPR bool operator<=(const U& x, const T& y)
  348. { return static_cast<bool>(y > x) || static_cast<bool>(y == x); }
  349. friend BOOST_OPERATORS_CONSTEXPR bool operator>=(const U& x, const T& y)
  350. { return static_cast<bool>(y < x) || static_cast<bool>(y == x); }
  351. };
  352. template <class T, class B = operators_detail::empty_base<T> >
  353. struct partially_ordered1 : B
  354. {
  355. friend BOOST_OPERATORS_CONSTEXPR bool operator>(const T& x, const T& y)
  356. { return y < x; }
  357. friend BOOST_OPERATORS_CONSTEXPR bool operator<=(const T& x, const T& y)
  358. { return static_cast<bool>(x < y) || static_cast<bool>(x == y); }
  359. friend BOOST_OPERATORS_CONSTEXPR bool operator>=(const T& x, const T& y)
  360. { return static_cast<bool>(y < x) || static_cast<bool>(x == y); }
  361. };
  362. // Combined operator classes (contributed by Daryle Walker) ----------------//
  363. template <class T, class U, class B = operators_detail::empty_base<T> >
  364. struct totally_ordered2
  365. : less_than_comparable2<T, U
  366. , equality_comparable2<T, U, B
  367. > > {};
  368. template <class T, class B = operators_detail::empty_base<T> >
  369. struct totally_ordered1
  370. : less_than_comparable1<T
  371. , equality_comparable1<T, B
  372. > > {};
  373. template <class T, class U, class B = operators_detail::empty_base<T> >
  374. struct additive2
  375. : addable2<T, U
  376. , subtractable2<T, U, B
  377. > > {};
  378. template <class T, class B = operators_detail::empty_base<T> >
  379. struct additive1
  380. : addable1<T
  381. , subtractable1<T, B
  382. > > {};
  383. template <class T, class U, class B = operators_detail::empty_base<T> >
  384. struct multiplicative2
  385. : multipliable2<T, U
  386. , dividable2<T, U, B
  387. > > {};
  388. template <class T, class B = operators_detail::empty_base<T> >
  389. struct multiplicative1
  390. : multipliable1<T
  391. , dividable1<T, B
  392. > > {};
  393. template <class T, class U, class B = operators_detail::empty_base<T> >
  394. struct integer_multiplicative2
  395. : multiplicative2<T, U
  396. , modable2<T, U, B
  397. > > {};
  398. template <class T, class B = operators_detail::empty_base<T> >
  399. struct integer_multiplicative1
  400. : multiplicative1<T
  401. , modable1<T, B
  402. > > {};
  403. template <class T, class U, class B = operators_detail::empty_base<T> >
  404. struct arithmetic2
  405. : additive2<T, U
  406. , multiplicative2<T, U, B
  407. > > {};
  408. template <class T, class B = operators_detail::empty_base<T> >
  409. struct arithmetic1
  410. : additive1<T
  411. , multiplicative1<T, B
  412. > > {};
  413. template <class T, class U, class B = operators_detail::empty_base<T> >
  414. struct integer_arithmetic2
  415. : additive2<T, U
  416. , integer_multiplicative2<T, U, B
  417. > > {};
  418. template <class T, class B = operators_detail::empty_base<T> >
  419. struct integer_arithmetic1
  420. : additive1<T
  421. , integer_multiplicative1<T, B
  422. > > {};
  423. template <class T, class U, class B = operators_detail::empty_base<T> >
  424. struct bitwise2
  425. : xorable2<T, U
  426. , andable2<T, U
  427. , orable2<T, U, B
  428. > > > {};
  429. template <class T, class B = operators_detail::empty_base<T> >
  430. struct bitwise1
  431. : xorable1<T
  432. , andable1<T
  433. , orable1<T, B
  434. > > > {};
  435. template <class T, class B = operators_detail::empty_base<T> >
  436. struct unit_steppable
  437. : incrementable<T
  438. , decrementable<T, B
  439. > > {};
  440. template <class T, class U, class B = operators_detail::empty_base<T> >
  441. struct shiftable2
  442. : left_shiftable2<T, U
  443. , right_shiftable2<T, U, B
  444. > > {};
  445. template <class T, class B = operators_detail::empty_base<T> >
  446. struct shiftable1
  447. : left_shiftable1<T
  448. , right_shiftable1<T, B
  449. > > {};
  450. template <class T, class U, class B = operators_detail::empty_base<T> >
  451. struct ring_operators2
  452. : additive2<T, U
  453. , subtractable2_left<T, U
  454. , multipliable2<T, U, B
  455. > > > {};
  456. template <class T, class B = operators_detail::empty_base<T> >
  457. struct ring_operators1
  458. : additive1<T
  459. , multipliable1<T, B
  460. > > {};
  461. template <class T, class U, class B = operators_detail::empty_base<T> >
  462. struct ordered_ring_operators2
  463. : ring_operators2<T, U
  464. , totally_ordered2<T, U, B
  465. > > {};
  466. template <class T, class B = operators_detail::empty_base<T> >
  467. struct ordered_ring_operators1
  468. : ring_operators1<T
  469. , totally_ordered1<T, B
  470. > > {};
  471. template <class T, class U, class B = operators_detail::empty_base<T> >
  472. struct field_operators2
  473. : ring_operators2<T, U
  474. , dividable2<T, U
  475. , dividable2_left<T, U, B
  476. > > > {};
  477. template <class T, class B = operators_detail::empty_base<T> >
  478. struct field_operators1
  479. : ring_operators1<T
  480. , dividable1<T, B
  481. > > {};
  482. template <class T, class U, class B = operators_detail::empty_base<T> >
  483. struct ordered_field_operators2
  484. : field_operators2<T, U
  485. , totally_ordered2<T, U, B
  486. > > {};
  487. template <class T, class B = operators_detail::empty_base<T> >
  488. struct ordered_field_operators1
  489. : field_operators1<T
  490. , totally_ordered1<T, B
  491. > > {};
  492. template <class T, class U, class B = operators_detail::empty_base<T> >
  493. struct euclidian_ring_operators2
  494. : ring_operators2<T, U
  495. , dividable2<T, U
  496. , dividable2_left<T, U
  497. , modable2<T, U
  498. , modable2_left<T, U, B
  499. > > > > > {};
  500. template <class T, class B = operators_detail::empty_base<T> >
  501. struct euclidian_ring_operators1
  502. : ring_operators1<T
  503. , dividable1<T
  504. , modable1<T, B
  505. > > > {};
  506. template <class T, class U, class B = operators_detail::empty_base<T> >
  507. struct ordered_euclidian_ring_operators2
  508. : totally_ordered2<T, U
  509. , euclidian_ring_operators2<T, U, B
  510. > > {};
  511. template <class T, class B = operators_detail::empty_base<T> >
  512. struct ordered_euclidian_ring_operators1
  513. : totally_ordered1<T
  514. , euclidian_ring_operators1<T, B
  515. > > {};
  516. template <class T, class U, class B = operators_detail::empty_base<T> >
  517. struct euclidean_ring_operators2
  518. : ring_operators2<T, U
  519. , dividable2<T, U
  520. , dividable2_left<T, U
  521. , modable2<T, U
  522. , modable2_left<T, U, B
  523. > > > > > {};
  524. template <class T, class B = operators_detail::empty_base<T> >
  525. struct euclidean_ring_operators1
  526. : ring_operators1<T
  527. , dividable1<T
  528. , modable1<T, B
  529. > > > {};
  530. template <class T, class U, class B = operators_detail::empty_base<T> >
  531. struct ordered_euclidean_ring_operators2
  532. : totally_ordered2<T, U
  533. , euclidean_ring_operators2<T, U, B
  534. > > {};
  535. template <class T, class B = operators_detail::empty_base<T> >
  536. struct ordered_euclidean_ring_operators1
  537. : totally_ordered1<T
  538. , euclidean_ring_operators1<T, B
  539. > > {};
  540. template <class T, class P, class B = operators_detail::empty_base<T> >
  541. struct input_iteratable
  542. : equality_comparable1<T
  543. , incrementable<T
  544. , dereferenceable<T, P, B
  545. > > > {};
  546. template <class T, class B = operators_detail::empty_base<T> >
  547. struct output_iteratable
  548. : incrementable<T, B
  549. > {};
  550. template <class T, class P, class B = operators_detail::empty_base<T> >
  551. struct forward_iteratable
  552. : input_iteratable<T, P, B
  553. > {};
  554. template <class T, class P, class B = operators_detail::empty_base<T> >
  555. struct bidirectional_iteratable
  556. : forward_iteratable<T, P
  557. , decrementable<T, B
  558. > > {};
  559. // To avoid repeated derivation from equality_comparable,
  560. // which is an indirect base class of bidirectional_iterable,
  561. // random_access_iteratable must not be derived from totally_ordered1
  562. // but from less_than_comparable1 only. (Helmut Zeisel, 02-Dec-2001)
  563. template <class T, class P, class D, class R, class B = operators_detail::empty_base<T> >
  564. struct random_access_iteratable
  565. : bidirectional_iteratable<T, P
  566. , less_than_comparable1<T
  567. , additive2<T, D
  568. , indexable<T, D, R, B
  569. > > > > {};
  570. //
  571. // Here's where we put it all together, defining the xxxx forms of the templates.
  572. // We also define specializations of is_chained_base<> for
  573. // the xxxx, xxxx1, and xxxx2 templates.
  574. //
  575. namespace operators_detail
  576. {
  577. // A type parameter is used instead of a plain bool because Borland's compiler
  578. // didn't cope well with the more obvious non-type template parameter.
  579. struct true_t {};
  580. struct false_t {};
  581. } // namespace operators_detail
  582. // is_chained_base<> - a traits class used to distinguish whether an operator
  583. // template argument is being used for base class chaining, or is specifying a
  584. // 2nd argument type.
  585. // Unspecialized version assumes that most types are not being used for base
  586. // class chaining. We specialize for the operator templates defined in this
  587. // library.
  588. template<class T> struct is_chained_base {
  589. typedef operators_detail::false_t value;
  590. };
  591. // Provide a specialization of 'is_chained_base<>'
  592. // for a 4-type-argument operator template.
  593. # define BOOST_OPERATOR_TEMPLATE4(template_name4) \
  594. template<class T, class U, class V, class W, class B> \
  595. struct is_chained_base< template_name4<T, U, V, W, B> > { \
  596. typedef operators_detail::true_t value; \
  597. };
  598. // Provide a specialization of 'is_chained_base<>'
  599. // for a 3-type-argument operator template.
  600. # define BOOST_OPERATOR_TEMPLATE3(template_name3) \
  601. template<class T, class U, class V, class B> \
  602. struct is_chained_base< template_name3<T, U, V, B> > { \
  603. typedef operators_detail::true_t value; \
  604. };
  605. // Provide a specialization of 'is_chained_base<>'
  606. // for a 2-type-argument operator template.
  607. # define BOOST_OPERATOR_TEMPLATE2(template_name2) \
  608. template<class T, class U, class B> \
  609. struct is_chained_base< template_name2<T, U, B> > { \
  610. typedef operators_detail::true_t value; \
  611. };
  612. // Provide a specialization of 'is_chained_base<>'
  613. // for a 1-type-argument operator template.
  614. # define BOOST_OPERATOR_TEMPLATE1(template_name1) \
  615. template<class T, class B> \
  616. struct is_chained_base< template_name1<T, B> > { \
  617. typedef operators_detail::true_t value; \
  618. };
  619. // BOOST_OPERATOR_TEMPLATE(template_name) defines template_name<> such that it
  620. // can be used for specifying both 1-argument and 2-argument forms. Requires the
  621. // existence of two previously defined class templates named '<template_name>1'
  622. // and '<template_name>2' which must implement the corresponding 1- and 2-
  623. // argument forms.
  624. //
  625. // The template type parameter O == is_chained_base<U>::value is used to
  626. // distinguish whether the 2nd argument to <template_name> is being used for
  627. // base class chaining from another boost operator template or is describing a
  628. // 2nd operand type. O == true_t only when U is actually an another operator
  629. // template from the library. Partial specialization is used to select an
  630. // implementation in terms of either '<template_name>1' or '<template_name>2'.
  631. //
  632. # define BOOST_OPERATOR_TEMPLATE(template_name) \
  633. template <class T \
  634. ,class U = T \
  635. ,class B = operators_detail::empty_base<T> \
  636. ,class O = typename is_chained_base<U>::value \
  637. > \
  638. struct template_name; \
  639. \
  640. template<class T, class U, class B> \
  641. struct template_name<T, U, B, operators_detail::false_t> \
  642. : template_name##2<T, U, B> {}; \
  643. \
  644. template<class T, class U> \
  645. struct template_name<T, U, operators_detail::empty_base<T>, operators_detail::true_t> \
  646. : template_name##1<T, U> {}; \
  647. \
  648. template <class T, class B> \
  649. struct template_name<T, T, B, operators_detail::false_t> \
  650. : template_name##1<T, B> {}; \
  651. \
  652. template<class T, class U, class B, class O> \
  653. struct is_chained_base< template_name<T, U, B, O> > { \
  654. typedef operators_detail::true_t value; \
  655. }; \
  656. \
  657. BOOST_OPERATOR_TEMPLATE2(template_name##2) \
  658. BOOST_OPERATOR_TEMPLATE1(template_name##1)
  659. BOOST_OPERATOR_TEMPLATE(less_than_comparable)
  660. BOOST_OPERATOR_TEMPLATE(equality_comparable)
  661. BOOST_OPERATOR_TEMPLATE(multipliable)
  662. BOOST_OPERATOR_TEMPLATE(addable)
  663. BOOST_OPERATOR_TEMPLATE(subtractable)
  664. BOOST_OPERATOR_TEMPLATE2(subtractable2_left)
  665. BOOST_OPERATOR_TEMPLATE(dividable)
  666. BOOST_OPERATOR_TEMPLATE2(dividable2_left)
  667. BOOST_OPERATOR_TEMPLATE(modable)
  668. BOOST_OPERATOR_TEMPLATE2(modable2_left)
  669. BOOST_OPERATOR_TEMPLATE(xorable)
  670. BOOST_OPERATOR_TEMPLATE(andable)
  671. BOOST_OPERATOR_TEMPLATE(orable)
  672. BOOST_OPERATOR_TEMPLATE1(incrementable)
  673. BOOST_OPERATOR_TEMPLATE1(decrementable)
  674. BOOST_OPERATOR_TEMPLATE2(dereferenceable)
  675. BOOST_OPERATOR_TEMPLATE3(indexable)
  676. BOOST_OPERATOR_TEMPLATE(left_shiftable)
  677. BOOST_OPERATOR_TEMPLATE(right_shiftable)
  678. BOOST_OPERATOR_TEMPLATE(equivalent)
  679. BOOST_OPERATOR_TEMPLATE(partially_ordered)
  680. BOOST_OPERATOR_TEMPLATE(totally_ordered)
  681. BOOST_OPERATOR_TEMPLATE(additive)
  682. BOOST_OPERATOR_TEMPLATE(multiplicative)
  683. BOOST_OPERATOR_TEMPLATE(integer_multiplicative)
  684. BOOST_OPERATOR_TEMPLATE(arithmetic)
  685. BOOST_OPERATOR_TEMPLATE(integer_arithmetic)
  686. BOOST_OPERATOR_TEMPLATE(bitwise)
  687. BOOST_OPERATOR_TEMPLATE1(unit_steppable)
  688. BOOST_OPERATOR_TEMPLATE(shiftable)
  689. BOOST_OPERATOR_TEMPLATE(ring_operators)
  690. BOOST_OPERATOR_TEMPLATE(ordered_ring_operators)
  691. BOOST_OPERATOR_TEMPLATE(field_operators)
  692. BOOST_OPERATOR_TEMPLATE(ordered_field_operators)
  693. BOOST_OPERATOR_TEMPLATE(euclidian_ring_operators)
  694. BOOST_OPERATOR_TEMPLATE(ordered_euclidian_ring_operators)
  695. BOOST_OPERATOR_TEMPLATE(euclidean_ring_operators)
  696. BOOST_OPERATOR_TEMPLATE(ordered_euclidean_ring_operators)
  697. BOOST_OPERATOR_TEMPLATE2(input_iteratable)
  698. BOOST_OPERATOR_TEMPLATE1(output_iteratable)
  699. BOOST_OPERATOR_TEMPLATE2(forward_iteratable)
  700. BOOST_OPERATOR_TEMPLATE2(bidirectional_iteratable)
  701. BOOST_OPERATOR_TEMPLATE4(random_access_iteratable)
  702. #undef BOOST_OPERATOR_TEMPLATE
  703. #undef BOOST_OPERATOR_TEMPLATE4
  704. #undef BOOST_OPERATOR_TEMPLATE3
  705. #undef BOOST_OPERATOR_TEMPLATE2
  706. #undef BOOST_OPERATOR_TEMPLATE1
  707. template <class T, class U>
  708. struct operators2
  709. : totally_ordered2<T,U
  710. , integer_arithmetic2<T,U
  711. , bitwise2<T,U
  712. > > > {};
  713. template <class T, class U = T>
  714. struct operators : operators2<T, U> {};
  715. template <class T> struct operators<T, T>
  716. : totally_ordered<T
  717. , integer_arithmetic<T
  718. , bitwise<T
  719. , unit_steppable<T
  720. > > > > {};
  721. // Iterator helper classes (contributed by Jeremy Siek) -------------------//
  722. // (Input and output iterator helpers contributed by Daryle Walker) -------//
  723. // (Changed to use combined operator classes by Daryle Walker) ------------//
  724. // (Adapted to C++17 by Daniel Frey) --------------------------------------//
  725. template <class Category,
  726. class T,
  727. class Distance = std::ptrdiff_t,
  728. class Pointer = T*,
  729. class Reference = T&>
  730. struct iterator_helper
  731. {
  732. typedef Category iterator_category;
  733. typedef T value_type;
  734. typedef Distance difference_type;
  735. typedef Pointer pointer;
  736. typedef Reference reference;
  737. };
  738. template <class T,
  739. class V,
  740. class D = std::ptrdiff_t,
  741. class P = V const *,
  742. class R = V const &>
  743. struct input_iterator_helper
  744. : input_iteratable<T, P
  745. , iterator_helper<std::input_iterator_tag, V, D, P, R
  746. > > {};
  747. template<class T>
  748. struct output_iterator_helper
  749. : output_iteratable<T
  750. , iterator_helper<std::output_iterator_tag, void, void, void, void
  751. > >
  752. {
  753. T& operator*() { return static_cast<T&>(*this); }
  754. T& operator++() { return static_cast<T&>(*this); }
  755. };
  756. template <class T,
  757. class V,
  758. class D = std::ptrdiff_t,
  759. class P = V*,
  760. class R = V&>
  761. struct forward_iterator_helper
  762. : forward_iteratable<T, P
  763. , iterator_helper<std::forward_iterator_tag, V, D, P, R
  764. > > {};
  765. template <class T,
  766. class V,
  767. class D = std::ptrdiff_t,
  768. class P = V*,
  769. class R = V&>
  770. struct bidirectional_iterator_helper
  771. : bidirectional_iteratable<T, P
  772. , iterator_helper<std::bidirectional_iterator_tag, V, D, P, R
  773. > > {};
  774. template <class T,
  775. class V,
  776. class D = std::ptrdiff_t,
  777. class P = V*,
  778. class R = V&>
  779. struct random_access_iterator_helper
  780. : random_access_iteratable<T, P, D, R
  781. , iterator_helper<std::random_access_iterator_tag, V, D, P, R
  782. > >
  783. {
  784. friend D requires_difference_operator(const T& x, const T& y) {
  785. return x - y;
  786. }
  787. }; // random_access_iterator_helper
  788. } // namespace operators_impl
  789. using namespace operators_impl;
  790. } // namespace boost
  791. #if defined(__sgi) && !defined(__GNUC__)
  792. #pragma reset woff 1234
  793. #endif
  794. #endif // BOOST_NO_OPERATORS_IN_NAMESPACE
  795. #endif // BOOST_OPERATORS_HPP