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

source code of boost/boost/operators.hpp