1 // Boost operators.hpp header file ----------------------------------------//
3 // (C) Copyright David Abrahams, Jeremy Siek, and Daryle Walker 1999-2001.
4 // Permission to copy, use, modify, sell and distribute this software is
5 // granted provided this copyright notice appears in all copies. This
6 // software is provided "as is" without express or implied warranty, and
7 // with no claim as to its suitability for any purpose.
9 // See http://www.boost.org/libs/utility/operators.htm for documentation.
12 // 21 Oct 02 Modified implementation of operators to allow compilers with a
13 // correct named return value optimization (NRVO) to produce optimal
14 // code. (Daniel Frey)
15 // 02 Dec 01 Bug fixed in random_access_iteratable. (Helmut Zeisel)
16 // 28 Sep 01 Factored out iterator operator groups. (Daryle Walker)
17 // 27 Aug 01 'left' form for non commutative operators added;
18 // additional classes for groups of related operators added;
19 // workaround for empty base class optimization
20 // bug of GCC 3.0 (Helmut Zeisel)
21 // 25 Jun 01 output_iterator_helper changes: removed default template
22 // parameters, added support for self-proxying, additional
23 // documentation and tests (Aleksey Gurtovoy)
24 // 29 May 01 Added operator classes for << and >>. Added input and output
25 // iterator helper classes. Added classes to connect equality and
26 // relational operators. Added classes for groups of related
27 // operators. Reimplemented example operator and iterator helper
28 // classes in terms of the new groups. (Daryle Walker, with help
29 // from Alexy Gurtovoy)
30 // 11 Feb 01 Fixed bugs in the iterator helpers which prevented explicitly
31 // supplied arguments from actually being used (Dave Abrahams)
32 // 04 Jul 00 Fixed NO_OPERATORS_IN_NAMESPACE bugs, major cleanup and
33 // refactoring of compiler workarounds, additional documentation
34 // (Alexy Gurtovoy and Mark Rodgers with some help and prompting from
36 // 28 Jun 00 General cleanup and integration of bugfixes from Mark Rodgers and
37 // Jeremy Siek (Dave Abrahams)
38 // 20 Jun 00 Changes to accommodate Borland C++Builder 4 and Borland C++ 5.5
40 // 20 Jun 00 Minor fixes to the prior revision (Aleksey Gurtovoy)
41 // 10 Jun 00 Support for the base class chaining technique was added
42 // (Aleksey Gurtovoy). See documentation and the comments below
44 // 12 Dec 99 Initial version with iterator operators (Jeremy Siek)
45 // 18 Nov 99 Change name "divideable" to "dividable", remove unnecessary
46 // specializations of dividable, subtractable, modable (Ed Brey)
47 // 17 Nov 99 Add comments (Beman Dawes)
48 // Remove unnecessary specialization of operators<> (Ed Brey)
49 // 15 Nov 99 Fix less_than_comparable<T,U> second operand type for first two
50 // operators.(Beman Dawes)
51 // 12 Nov 99 Add operators templates (Ed Brey)
52 // 11 Nov 99 Add single template parameter version for compilers without
53 // partial specialization (Beman Dawes)
54 // 10 Nov 99 Initial version
57 // An additional optional template parameter was added to most of
58 // operator templates to support the base class chaining technique (see
59 // documentation for the details). Unfortunately, a straightforward
60 // implementation of this change would have broken compatibility with the
61 // previous version of the library by making it impossible to use the same
62 // template name (e.g. 'addable') for both the 1- and 2-argument versions of
63 // an operator template. This implementation solves the backward-compatibility
64 // issue at the cost of some simplicity.
66 // One of the complications is an existence of special auxiliary class template
67 // 'is_chained_base<>' (see 'detail' namespace below), which is used
68 // to determine whether its template parameter is a library's operator template
69 // or not. You have to specialize 'is_chained_base<>' for each new
70 // operator template you add to the library.
72 // However, most of the non-trivial implementation details are hidden behind
73 // several local macros defined below, and as soon as you understand them,
74 // you understand the whole library implementation.
76 #ifndef BOOST_OPERATORS_HPP
77 #define BOOST_OPERATORS_HPP
79 #include <boost/config.hpp>
80 #include <boost/iterator.hpp>
82 #if defined(__sgi) && !defined(__GNUC__)
86 #if defined(BOOST_MSVC)
87 # pragma warning( disable : 4284 ) // complaint about return type of
88 #endif // operator-> not begin a UDT
93 // Helmut Zeisel, empty base class optimization bug with GCC 3.0.0
94 #if defined(__GNUC__) && __GNUC__==3 && __GNUC_MINOR__==0 && __GNU_PATCHLEVEL__==0
102 } // namespace detail
105 // In this section we supply the xxxx1 and xxxx2 forms of the operator
106 // templates, which are explicitly targeted at the 1-type-argument and
107 // 2-type-argument operator forms, respectively. Some compilers get confused
108 // when inline friend functions are overloaded in namespaces other than the
109 // global namespace. When BOOST_NO_OPERATORS_IN_NAMESPACE is defined, all of
110 // these templates must go in the global namespace.
112 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
117 // 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
122 template <class T, class U, class B = ::boost::detail::empty_base>
123 struct less_than_comparable2 : B
125 friend bool operator<=(const T& x, const U& y) { return !(x > y); }
126 friend bool operator>=(const T& x, const U& y) { return !(x < y); }
127 friend bool operator>(const U& x, const T& y) { return y < x; }
128 friend bool operator<(const U& x, const T& y) { return y > x; }
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); }
133 template <class T, class B = ::boost::detail::empty_base>
134 struct less_than_comparable1 : B
136 friend bool operator>(const T& x, const T& y) { return y < x; }
137 friend bool operator<=(const T& x, const T& y) { return !(y < x); }
138 friend bool operator>=(const T& x, const T& y) { return !(x < y); }
141 template <class T, class U, class B = ::boost::detail::empty_base>
142 struct equality_comparable2 : B
144 friend bool operator==(const U& y, const T& x) { return x == y; }
145 friend bool operator!=(const U& y, const T& x) { return !(x == y); }
146 friend bool operator!=(const T& y, const U& x) { return !(y == x); }
149 template <class T, class B = ::boost::detail::empty_base>
150 struct equality_comparable1 : B
152 friend bool operator!=(const T& x, const T& y) { return !(x == y); }
155 // NRVO-friendly implementation (contributed by Daniel Frey) ---------------//
157 #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
159 // This is the optimal implementation for ISO/ANSI C++,
160 // but it requires the compiler to implement the NRVO.
161 // If the compiler has no NRVO, this is the best symmetric
162 // implementation available.
164 #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \
165 template <class T, class U, class B = ::boost::detail::empty_base> \
168 friend T operator OP( const T& lhs, const U& rhs ) \
169 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
170 friend T operator OP( const U& lhs, const T& rhs ) \
171 { T nrv( rhs ); nrv OP##= lhs; return nrv; } \
174 template <class T, class B = ::boost::detail::empty_base> \
177 friend T operator OP( const T& lhs, const T& rhs ) \
178 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
181 #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \
182 template <class T, class U, class B = ::boost::detail::empty_base> \
185 friend T operator OP( const T& lhs, const U& rhs ) \
186 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
189 template <class T, class U, class B = ::boost::detail::empty_base> \
190 struct NAME##2_left : B \
192 friend T operator OP( const U& lhs, const T& rhs ) \
193 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
196 template <class T, class B = ::boost::detail::empty_base> \
199 friend T operator OP( const T& lhs, const T& rhs ) \
200 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
203 #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
205 // For compilers without NRVO the following code is optimal, but not symmetric!
206 // Note that the implementation of NAME##2_left only looks cool, but doesn't
207 // provide optimization opportunities to the compiler :)
209 #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \
210 template <class T, class U, class B = ::boost::detail::empty_base> \
213 friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
214 friend T operator OP( const U& lhs, T rhs ) { return rhs OP##= lhs; } \
217 template <class T, class B = ::boost::detail::empty_base> \
220 friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
223 #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \
224 template <class T, class U, class B = ::boost::detail::empty_base> \
227 friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
230 template <class T, class U, class B = ::boost::detail::empty_base> \
231 struct NAME##2_left : B \
233 friend T operator OP( const U& lhs, const T& rhs ) \
234 { return T( lhs ) OP##= rhs; } \
237 template <class T, class B = ::boost::detail::empty_base> \
240 friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
243 #endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
245 BOOST_BINARY_OPERATOR_COMMUTATIVE( multipliable, * )
246 BOOST_BINARY_OPERATOR_COMMUTATIVE( addable, + )
247 BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( subtractable, - )
248 BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( dividable, / )
249 BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( modable, % )
250 BOOST_BINARY_OPERATOR_COMMUTATIVE( xorable, ^ )
251 BOOST_BINARY_OPERATOR_COMMUTATIVE( andable, & )
252 BOOST_BINARY_OPERATOR_COMMUTATIVE( orable, | )
254 #undef BOOST_BINARY_OPERATOR_COMMUTATIVE
255 #undef BOOST_BINARY_OPERATOR_NON_COMMUTATIVE
257 // incrementable and decrementable contributed by Jeremy Siek
259 template <class T, class B = ::boost::detail::empty_base>
260 struct incrementable : B
262 friend T operator++(T& x, int)
264 incrementable_type nrv(x);
268 private: // The use of this typedef works around a Borland bug
269 typedef T incrementable_type;
272 template <class T, class B = ::boost::detail::empty_base>
273 struct decrementable : B
275 friend T operator--(T& x, int)
277 decrementable_type nrv(x);
281 private: // The use of this typedef works around a Borland bug
282 typedef T decrementable_type;
285 // Iterator operator classes (contributed by Jeremy Siek) ------------------//
287 template <class T, class P, class B = ::boost::detail::empty_base>
288 struct dereferenceable : B
292 return &*static_cast<const T&>(*this);
296 template <class T, class I, class R, class B = ::boost::detail::empty_base>
299 R operator[](I n) const
301 return *(static_cast<const T&>(*this) + n);
305 // More operator classes (contributed by Daryle Walker) --------------------//
306 // (NRVO-friendly implementation contributed by Daniel Frey) ---------------//
308 #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
310 #define BOOST_BINARY_OPERATOR( NAME, OP ) \
311 template <class T, class U, class B = ::boost::detail::empty_base> \
314 friend T operator OP( const T& lhs, const U& rhs ) \
315 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
318 template <class T, class B = ::boost::detail::empty_base> \
321 friend T operator OP( const T& lhs, const T& rhs ) \
322 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
325 #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
327 #define BOOST_BINARY_OPERATOR( NAME, OP ) \
328 template <class T, class U, class B = ::boost::detail::empty_base> \
331 friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
334 template <class T, class B = ::boost::detail::empty_base> \
337 friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
340 #endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
342 BOOST_BINARY_OPERATOR( left_shiftable, << )
343 BOOST_BINARY_OPERATOR( right_shiftable, >> )
345 #undef BOOST_BINARY_OPERATOR
347 template <class T, class U, class B = ::boost::detail::empty_base>
348 struct equivalent2 : B
350 friend bool operator==(const T& x, const U& y)
352 return !(x < y) && !(x > y);
356 template <class T, class B = ::boost::detail::empty_base>
357 struct equivalent1 : B
359 friend bool operator==(const T&x, const T&y)
361 return !(x < y) && !(y < x);
365 template <class T, class U, class B = ::boost::detail::empty_base>
366 struct partially_ordered2 : B
368 friend bool operator<=(const T& x, const U& y)
369 { return (x < y) || (x == y); }
370 friend bool operator>=(const T& x, const U& y)
371 { return (x > y) || (x == y); }
372 friend bool operator>(const U& x, const T& y)
374 friend bool operator<(const U& x, const T& y)
376 friend bool operator<=(const U& x, const T& y)
377 { return (y > x) || (y == x); }
378 friend bool operator>=(const U& x, const T& y)
379 { return (y < x) || (y == x); }
382 template <class T, class B = ::boost::detail::empty_base>
383 struct partially_ordered1 : B
385 friend bool operator>(const T& x, const T& y)
387 friend bool operator<=(const T& x, const T& y)
388 { return (x < y) || (x == y); }
389 friend bool operator>=(const T& x, const T& y)
390 { return (y < x) || (x == y); }
393 // Combined operator classes (contributed by Daryle Walker) ----------------//
395 template <class T, class U, class B = ::boost::detail::empty_base>
396 struct totally_ordered2
397 : less_than_comparable2<T, U
398 , equality_comparable2<T, U, B
401 template <class T, class B = ::boost::detail::empty_base>
402 struct totally_ordered1
403 : less_than_comparable1<T
404 , equality_comparable1<T, B
407 template <class T, class U, class B = ::boost::detail::empty_base>
410 , subtractable2<T, U, B
413 template <class T, class B = ::boost::detail::empty_base>
419 template <class T, class U, class B = ::boost::detail::empty_base>
420 struct multiplicative2
425 template <class T, class B = ::boost::detail::empty_base>
426 struct multiplicative1
431 template <class T, class U, class B = ::boost::detail::empty_base>
432 struct integer_multiplicative2
433 : multiplicative2<T, U
437 template <class T, class B = ::boost::detail::empty_base>
438 struct integer_multiplicative1
443 template <class T, class U, class B = ::boost::detail::empty_base>
446 , multiplicative2<T, U, B
449 template <class T, class B = ::boost::detail::empty_base>
452 , multiplicative1<T, B
455 template <class T, class U, class B = ::boost::detail::empty_base>
456 struct integer_arithmetic2
458 , integer_multiplicative2<T, U, B
461 template <class T, class B = ::boost::detail::empty_base>
462 struct integer_arithmetic1
464 , integer_multiplicative1<T, B
467 template <class T, class U, class B = ::boost::detail::empty_base>
474 template <class T, class B = ::boost::detail::empty_base>
481 template <class T, class B = ::boost::detail::empty_base>
482 struct unit_steppable
487 template <class T, class U, class B = ::boost::detail::empty_base>
489 : left_shiftable2<T, U
490 , right_shiftable2<T, U, B
493 template <class T, class B = ::boost::detail::empty_base>
496 , right_shiftable1<T, B
499 template <class T, class U, class B = ::boost::detail::empty_base>
500 struct ring_operators2
502 , subtractable2_left<T, U
503 , multipliable2<T, U, B
506 template <class T, class B = ::boost::detail::empty_base>
507 struct ring_operators1
512 template <class T, class U, class B = ::boost::detail::empty_base>
513 struct ordered_ring_operators2
514 : ring_operators2<T, U
515 , totally_ordered2<T, U, B
518 template <class T, class B = ::boost::detail::empty_base>
519 struct ordered_ring_operators1
521 , totally_ordered1<T, B
524 template <class T, class U, class B = ::boost::detail::empty_base>
525 struct field_operators2
526 : ring_operators2<T, U
528 , dividable2_left<T, U, B
531 template <class T, class B = ::boost::detail::empty_base>
532 struct field_operators1
537 template <class T, class U, class B = ::boost::detail::empty_base>
538 struct ordered_field_operators2
539 : field_operators2<T, U
540 , totally_ordered2<T, U, B
543 template <class T, class B = ::boost::detail::empty_base>
544 struct ordered_field_operators1
546 , totally_ordered1<T, B
549 template <class T, class U, class B = ::boost::detail::empty_base>
550 struct euclidian_ring_operators2
551 : ring_operators2<T, U
553 , dividable2_left<T, U
555 , modable2_left<T, U, B
558 template <class T, class B = ::boost::detail::empty_base>
559 struct euclidian_ring_operators1
565 template <class T, class U, class B = ::boost::detail::empty_base>
566 struct ordered_euclidian_ring_operators2
567 : totally_ordered2<T, U
568 , euclidian_ring_operators2<T, U, B
571 template <class T, class B = ::boost::detail::empty_base>
572 struct ordered_euclidian_ring_operators1
574 , euclidian_ring_operators1<T, B
577 template <class T, class P, class B = ::boost::detail::empty_base>
578 struct input_iteratable
579 : equality_comparable1<T
581 , dereferenceable<T, P, B
584 template <class T, class B = ::boost::detail::empty_base>
585 struct output_iteratable
589 template <class T, class P, class B = ::boost::detail::empty_base>
590 struct forward_iteratable
591 : input_iteratable<T, P, B
594 template <class T, class P, class B = ::boost::detail::empty_base>
595 struct bidirectional_iteratable
596 : forward_iteratable<T, P
600 // To avoid repeated derivation from equality_comparable,
601 // which is an indirect base class of bidirectional_iterable,
602 // random_access_iteratable must not be derived from totally_ordered1
603 // but from less_than_comparable1 only. (Helmut Zeisel, 02-Dec-2001)
604 template <class T, class P, class D, class R, class B = ::boost::detail::empty_base>
605 struct random_access_iteratable
606 : bidirectional_iteratable<T, P
607 , less_than_comparable1<T
609 , indexable<T, D, R, B
612 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
614 #endif // BOOST_NO_OPERATORS_IN_NAMESPACE
617 // BOOST_IMPORT_TEMPLATE1 .. BOOST_IMPORT_TEMPLATE4 -
619 // When BOOST_NO_OPERATORS_IN_NAMESPACE is defined we need a way to import an
620 // operator template into the boost namespace. BOOST_IMPORT_TEMPLATE1 is used
621 // for one-argument forms of operator templates; BOOST_IMPORT_TEMPLATE2 for
622 // two-argument forms. Note that these macros expect to be invoked from within
625 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
627 // The template is already in boost so we have nothing to do.
628 # define BOOST_IMPORT_TEMPLATE4(template_name)
629 # define BOOST_IMPORT_TEMPLATE3(template_name)
630 # define BOOST_IMPORT_TEMPLATE2(template_name)
631 # define BOOST_IMPORT_TEMPLATE1(template_name)
633 #else // BOOST_NO_OPERATORS_IN_NAMESPACE
635 # ifndef BOOST_NO_USING_TEMPLATE
637 // Bring the names in with a using-declaration
638 // to avoid stressing the compiler.
639 # define BOOST_IMPORT_TEMPLATE4(template_name) using ::template_name;
640 # define BOOST_IMPORT_TEMPLATE3(template_name) using ::template_name;
641 # define BOOST_IMPORT_TEMPLATE2(template_name) using ::template_name;
642 # define BOOST_IMPORT_TEMPLATE1(template_name) using ::template_name;
646 // Otherwise, because a Borland C++ 5.5 bug prevents a using declaration
647 // from working, we are forced to use inheritance for that compiler.
648 # define BOOST_IMPORT_TEMPLATE4(template_name) \
649 template <class T, class U, class V, class W, class B = ::boost::detail::empty_base> \
650 struct template_name : ::template_name<T, U, V, W, B> {};
652 # define BOOST_IMPORT_TEMPLATE3(template_name) \
653 template <class T, class U, class V, class B = ::boost::detail::empty_base> \
654 struct template_name : ::template_name<T, U, V, B> {};
656 # define BOOST_IMPORT_TEMPLATE2(template_name) \
657 template <class T, class U, class B = ::boost::detail::empty_base> \
658 struct template_name : ::template_name<T, U, B> {};
660 # define BOOST_IMPORT_TEMPLATE1(template_name) \
661 template <class T, class B = ::boost::detail::empty_base> \
662 struct template_name : ::template_name<T, B> {};
664 # endif // BOOST_NO_USING_TEMPLATE
666 #endif // BOOST_NO_OPERATORS_IN_NAMESPACE
669 // Here's where we put it all together, defining the xxxx forms of the templates
670 // in namespace boost. We also define specializations of is_chained_base<> for
671 // the xxxx, xxxx1, and xxxx2 templates, importing them into boost:: as
674 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
676 // is_chained_base<> - a traits class used to distinguish whether an operator
677 // template argument is being used for base class chaining, or is specifying a
678 // 2nd argument type.
681 // A type parameter is used instead of a plain bool because Borland's compiler
682 // didn't cope well with the more obvious non-type template parameter.
686 } // namespace detail
688 // Unspecialized version assumes that most types are not being used for base
689 // class chaining. We specialize for the operator templates defined in this
691 template<class T> struct is_chained_base {
692 typedef ::boost::detail::false_t value;
697 // Import a 4-type-argument operator template into boost (if neccessary) and
698 // provide a specialization of 'is_chained_base<>' for it.
699 # define BOOST_OPERATOR_TEMPLATE4(template_name4) \
700 BOOST_IMPORT_TEMPLATE4(template_name4) \
701 template<class T, class U, class V, class W, class B> \
702 struct is_chained_base< ::boost::template_name4<T, U, V, W, B> > { \
703 typedef ::boost::detail::true_t value; \
706 // Import a 3-type-argument operator template into boost (if neccessary) and
707 // provide a specialization of 'is_chained_base<>' for it.
708 # define BOOST_OPERATOR_TEMPLATE3(template_name3) \
709 BOOST_IMPORT_TEMPLATE3(template_name3) \
710 template<class T, class U, class V, class B> \
711 struct is_chained_base< ::boost::template_name3<T, U, V, B> > { \
712 typedef ::boost::detail::true_t value; \
715 // Import a 2-type-argument operator template into boost (if neccessary) and
716 // provide a specialization of 'is_chained_base<>' for it.
717 # define BOOST_OPERATOR_TEMPLATE2(template_name2) \
718 BOOST_IMPORT_TEMPLATE2(template_name2) \
719 template<class T, class U, class B> \
720 struct is_chained_base< ::boost::template_name2<T, U, B> > { \
721 typedef ::boost::detail::true_t value; \
724 // Import a 1-type-argument operator template into boost (if neccessary) and
725 // provide a specialization of 'is_chained_base<>' for it.
726 # define BOOST_OPERATOR_TEMPLATE1(template_name1) \
727 BOOST_IMPORT_TEMPLATE1(template_name1) \
728 template<class T, class B> \
729 struct is_chained_base< ::boost::template_name1<T, B> > { \
730 typedef ::boost::detail::true_t value; \
733 // BOOST_OPERATOR_TEMPLATE(template_name) defines template_name<> such that it
734 // can be used for specifying both 1-argument and 2-argument forms. Requires the
735 // existence of two previously defined class templates named '<template_name>1'
736 // and '<template_name>2' which must implement the corresponding 1- and 2-
739 // The template type parameter O == is_chained_base<U>::value is used to
740 // distinguish whether the 2nd argument to <template_name> is being used for
741 // base class chaining from another boost operator template or is describing a
742 // 2nd operand type. O == true_t only when U is actually an another operator
743 // template from the library. Partial specialization is used to select an
744 // implementation in terms of either '<template_name>1' or '<template_name>2'.
747 # define BOOST_OPERATOR_TEMPLATE(template_name) \
750 ,class B = ::boost::detail::empty_base \
751 ,class O = typename is_chained_base<U>::value \
753 struct template_name : template_name##2<T, U, B> {}; \
755 template<class T, class U, class B> \
756 struct template_name<T, U, B, ::boost::detail::true_t> \
757 : template_name##1<T, U> {}; \
759 template <class T, class B> \
760 struct template_name<T, T, B, ::boost::detail::false_t> \
761 : template_name##1<T, B> {}; \
763 template<class T, class U, class B, class O> \
764 struct is_chained_base< ::boost::template_name<T, U, B, O> > { \
765 typedef ::boost::detail::true_t value; \
768 BOOST_OPERATOR_TEMPLATE2(template_name##2) \
769 BOOST_OPERATOR_TEMPLATE1(template_name##1)
772 #else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
774 # define BOOST_OPERATOR_TEMPLATE4(template_name4) \
775 BOOST_IMPORT_TEMPLATE4(template_name4)
776 # define BOOST_OPERATOR_TEMPLATE3(template_name3) \
777 BOOST_IMPORT_TEMPLATE3(template_name3)
778 # define BOOST_OPERATOR_TEMPLATE2(template_name2) \
779 BOOST_IMPORT_TEMPLATE2(template_name2)
780 # define BOOST_OPERATOR_TEMPLATE1(template_name1) \
781 BOOST_IMPORT_TEMPLATE1(template_name1)
783 // In this case we can only assume that template_name<> is equivalent to the
784 // more commonly needed template_name1<> form.
785 # define BOOST_OPERATOR_TEMPLATE(template_name) \
786 template <class T, class B = ::boost::detail::empty_base> \
787 struct template_name : template_name##1<T, B> {};
789 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
793 BOOST_OPERATOR_TEMPLATE(less_than_comparable)
794 BOOST_OPERATOR_TEMPLATE(equality_comparable)
795 BOOST_OPERATOR_TEMPLATE(multipliable)
796 BOOST_OPERATOR_TEMPLATE(addable)
797 BOOST_OPERATOR_TEMPLATE(subtractable)
798 BOOST_OPERATOR_TEMPLATE2(subtractable2_left)
799 BOOST_OPERATOR_TEMPLATE(dividable)
800 BOOST_OPERATOR_TEMPLATE2(dividable2_left)
801 BOOST_OPERATOR_TEMPLATE(modable)
802 BOOST_OPERATOR_TEMPLATE2(modable2_left)
803 BOOST_OPERATOR_TEMPLATE(xorable)
804 BOOST_OPERATOR_TEMPLATE(andable)
805 BOOST_OPERATOR_TEMPLATE(orable)
807 BOOST_OPERATOR_TEMPLATE1(incrementable)
808 BOOST_OPERATOR_TEMPLATE1(decrementable)
810 BOOST_OPERATOR_TEMPLATE2(dereferenceable)
811 BOOST_OPERATOR_TEMPLATE3(indexable)
813 BOOST_OPERATOR_TEMPLATE(left_shiftable)
814 BOOST_OPERATOR_TEMPLATE(right_shiftable)
815 BOOST_OPERATOR_TEMPLATE(equivalent)
816 BOOST_OPERATOR_TEMPLATE(partially_ordered)
818 BOOST_OPERATOR_TEMPLATE(totally_ordered)
819 BOOST_OPERATOR_TEMPLATE(additive)
820 BOOST_OPERATOR_TEMPLATE(multiplicative)
821 BOOST_OPERATOR_TEMPLATE(integer_multiplicative)
822 BOOST_OPERATOR_TEMPLATE(arithmetic)
823 BOOST_OPERATOR_TEMPLATE(integer_arithmetic)
824 BOOST_OPERATOR_TEMPLATE(bitwise)
825 BOOST_OPERATOR_TEMPLATE1(unit_steppable)
826 BOOST_OPERATOR_TEMPLATE(shiftable)
827 BOOST_OPERATOR_TEMPLATE(ring_operators)
828 BOOST_OPERATOR_TEMPLATE(ordered_ring_operators)
829 BOOST_OPERATOR_TEMPLATE(field_operators)
830 BOOST_OPERATOR_TEMPLATE(ordered_field_operators)
831 BOOST_OPERATOR_TEMPLATE(euclidian_ring_operators)
832 BOOST_OPERATOR_TEMPLATE(ordered_euclidian_ring_operators)
833 BOOST_OPERATOR_TEMPLATE2(input_iteratable)
834 BOOST_OPERATOR_TEMPLATE1(output_iteratable)
835 BOOST_OPERATOR_TEMPLATE2(forward_iteratable)
836 BOOST_OPERATOR_TEMPLATE2(bidirectional_iteratable)
837 BOOST_OPERATOR_TEMPLATE4(random_access_iteratable)
839 #undef BOOST_OPERATOR_TEMPLATE
840 #undef BOOST_OPERATOR_TEMPLATE4
841 #undef BOOST_OPERATOR_TEMPLATE3
842 #undef BOOST_OPERATOR_TEMPLATE2
843 #undef BOOST_OPERATOR_TEMPLATE1
844 #undef BOOST_IMPORT_TEMPLATE1
845 #undef BOOST_IMPORT_TEMPLATE2
846 #undef BOOST_IMPORT_TEMPLATE3
847 #undef BOOST_IMPORT_TEMPLATE4
849 // The following 'operators' classes can only be used portably if the derived class
850 // declares ALL of the required member operators.
851 template <class T, class U>
853 : totally_ordered2<T,U
854 , integer_arithmetic2<T,U
858 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
859 template <class T, class U = T>
860 struct operators : operators2<T, U> {};
862 template <class T> struct operators<T, T>
864 template <class T> struct operators
867 , integer_arithmetic<T
872 // Iterator helper classes (contributed by Jeremy Siek) -------------------//
873 // (Input and output iterator helpers contributed by Daryle Walker) -------//
874 // (Changed to use combined operator classes by Daryle Walker) ------------//
877 class D = std::ptrdiff_t,
880 struct input_iterator_helper
881 : input_iteratable<T, P
882 , boost::iterator<std::input_iterator_tag, V, D, P, R
886 struct output_iterator_helper
887 : output_iteratable<T
888 , boost::iterator<std::output_iterator_tag, void, void, void, void
891 T& operator*() { return static_cast<T&>(*this); }
892 T& operator++() { return static_cast<T&>(*this); }
897 class D = std::ptrdiff_t,
900 struct forward_iterator_helper
901 : forward_iteratable<T, P
902 , boost::iterator<std::forward_iterator_tag, V, D, P, R
907 class D = std::ptrdiff_t,
910 struct bidirectional_iterator_helper
911 : bidirectional_iteratable<T, P
912 , boost::iterator<std::bidirectional_iterator_tag, V, D, P, R
917 class D = std::ptrdiff_t,
920 struct random_access_iterator_helper
921 : random_access_iteratable<T, P, D, R
922 , boost::iterator<std::random_access_iterator_tag, V, D, P, R
925 friend D requires_difference_operator(const T& x, const T& y) {
928 }; // random_access_iterator_helper
932 #if defined(__sgi) && !defined(__GNUC__)
933 #pragma reset woff 1234
936 #endif // BOOST_OPERATORS_HPP