1 // Boost operators.hpp header file ----------------------------------------//
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)
8 // See http://www.boost.org/libs/utility/operators.htm for documentation.
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
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
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
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
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.
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.
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.
82 #ifndef BOOST_OPERATORS_HPP
83 #define BOOST_OPERATORS_HPP
85 #include <boost/config.hpp>
86 #include <boost/iterator.hpp>
87 #include <boost/detail/workaround.hpp>
89 #if defined(__sgi) && !defined(__GNUC__)
90 # pragma set woff 1234
93 #if BOOST_WORKAROUND(BOOST_MSVC, < 1600)
94 # pragma warning( disable : 4284 ) // complaint about return type of
95 #endif // operator-> not begin a UDT
100 template <typename T> class empty_base {};
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<T> >
123 struct less_than_comparable2 : B
125 friend bool operator<=(const T& x, const U& y) { return !static_cast<bool>(x > y); }
126 friend bool operator>=(const T& x, const U& y) { return !static_cast<bool>(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 !static_cast<bool>(y < x); }
130 friend bool operator>=(const U& x, const T& y) { return !static_cast<bool>(y > x); }
133 template <class T, class B = ::boost::detail::empty_base<T> >
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 !static_cast<bool>(y < x); }
138 friend bool operator>=(const T& x, const T& y) { return !static_cast<bool>(x < y); }
141 template <class T, class U, class B = ::boost::detail::empty_base<T> >
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 !static_cast<bool>(x == y); }
146 friend bool operator!=(const T& y, const U& x) { return !static_cast<bool>(y == x); }
149 template <class T, class B = ::boost::detail::empty_base<T> >
150 struct equality_comparable1 : B
152 friend bool operator!=(const T& x, const T& y) { return !static_cast<bool>(x == y); }
155 // A macro which produces "name_2left" from "name".
156 #define BOOST_OPERATOR2_LEFT(name) name##2##_##left
158 // NRVO-friendly implementation (contributed by Daniel Frey) ---------------//
160 #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
162 // This is the optimal implementation for ISO/ANSI C++,
163 // but it requires the compiler to implement the NRVO.
164 // If the compiler has no NRVO, this is the best symmetric
165 // implementation available.
167 #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \
168 template <class T, class U, class B = ::boost::detail::empty_base<T> > \
171 friend T operator OP( const T& lhs, const U& rhs ) \
172 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
173 friend T operator OP( const U& lhs, const T& rhs ) \
174 { T nrv( rhs ); nrv OP##= lhs; return nrv; } \
177 template <class T, class B = ::boost::detail::empty_base<T> > \
180 friend T operator OP( const T& lhs, const T& rhs ) \
181 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
184 #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \
185 template <class T, class U, class B = ::boost::detail::empty_base<T> > \
188 friend T operator OP( const T& lhs, const U& rhs ) \
189 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
192 template <class T, class U, class B = ::boost::detail::empty_base<T> > \
193 struct BOOST_OPERATOR2_LEFT(NAME) : B \
195 friend T operator OP( const U& lhs, const T& rhs ) \
196 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
199 template <class T, class B = ::boost::detail::empty_base<T> > \
202 friend T operator OP( const T& lhs, const T& rhs ) \
203 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
206 #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
208 // For compilers without NRVO the following code is optimal, but not
209 // symmetric! Note that the implementation of
210 // BOOST_OPERATOR2_LEFT(NAME) only looks cool, but doesn't provide
211 // optimization opportunities to the compiler :)
213 #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \
214 template <class T, class U, class B = ::boost::detail::empty_base<T> > \
217 friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
218 friend T operator OP( const U& lhs, T rhs ) { return rhs OP##= lhs; } \
221 template <class T, class B = ::boost::detail::empty_base<T> > \
224 friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
227 #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \
228 template <class T, class U, class B = ::boost::detail::empty_base<T> > \
231 friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
234 template <class T, class U, class B = ::boost::detail::empty_base<T> > \
235 struct BOOST_OPERATOR2_LEFT(NAME) : B \
237 friend T operator OP( const U& lhs, const T& rhs ) \
238 { return T( lhs ) OP##= rhs; } \
241 template <class T, class B = ::boost::detail::empty_base<T> > \
244 friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
247 #endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
249 BOOST_BINARY_OPERATOR_COMMUTATIVE( multipliable, * )
250 BOOST_BINARY_OPERATOR_COMMUTATIVE( addable, + )
251 BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( subtractable, - )
252 BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( dividable, / )
253 BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( modable, % )
254 BOOST_BINARY_OPERATOR_COMMUTATIVE( xorable, ^ )
255 BOOST_BINARY_OPERATOR_COMMUTATIVE( andable, & )
256 BOOST_BINARY_OPERATOR_COMMUTATIVE( orable, | )
258 #undef BOOST_BINARY_OPERATOR_COMMUTATIVE
259 #undef BOOST_BINARY_OPERATOR_NON_COMMUTATIVE
260 #undef BOOST_OPERATOR2_LEFT
262 // incrementable and decrementable contributed by Jeremy Siek
264 template <class T, class B = ::boost::detail::empty_base<T> >
265 struct incrementable : B
267 friend T operator++(T& x, int)
269 incrementable_type nrv(x);
273 private: // The use of this typedef works around a Borland bug
274 typedef T incrementable_type;
277 template <class T, class B = ::boost::detail::empty_base<T> >
278 struct decrementable : B
280 friend T operator--(T& x, int)
282 decrementable_type nrv(x);
286 private: // The use of this typedef works around a Borland bug
287 typedef T decrementable_type;
290 // Iterator operator classes (contributed by Jeremy Siek) ------------------//
292 template <class T, class P, class B = ::boost::detail::empty_base<T> >
293 struct dereferenceable : B
297 return &*static_cast<const T&>(*this);
301 template <class T, class I, class R, class B = ::boost::detail::empty_base<T> >
304 R operator[](I n) const
306 return *(static_cast<const T&>(*this) + n);
310 // More operator classes (contributed by Daryle Walker) --------------------//
311 // (NRVO-friendly implementation contributed by Daniel Frey) ---------------//
313 #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
315 #define BOOST_BINARY_OPERATOR( NAME, OP ) \
316 template <class T, class U, class B = ::boost::detail::empty_base<T> > \
319 friend T operator OP( const T& lhs, const U& rhs ) \
320 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
323 template <class T, class B = ::boost::detail::empty_base<T> > \
326 friend T operator OP( const T& lhs, const T& rhs ) \
327 { T nrv( lhs ); nrv OP##= rhs; return nrv; } \
330 #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
332 #define BOOST_BINARY_OPERATOR( NAME, OP ) \
333 template <class T, class U, class B = ::boost::detail::empty_base<T> > \
336 friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \
339 template <class T, class B = ::boost::detail::empty_base<T> > \
342 friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \
345 #endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS)
347 BOOST_BINARY_OPERATOR( left_shiftable, << )
348 BOOST_BINARY_OPERATOR( right_shiftable, >> )
350 #undef BOOST_BINARY_OPERATOR
352 template <class T, class U, class B = ::boost::detail::empty_base<T> >
353 struct equivalent2 : B
355 friend bool operator==(const T& x, const U& y)
357 return !static_cast<bool>(x < y) && !static_cast<bool>(x > y);
361 template <class T, class B = ::boost::detail::empty_base<T> >
362 struct equivalent1 : B
364 friend bool operator==(const T&x, const T&y)
366 return !static_cast<bool>(x < y) && !static_cast<bool>(y < x);
370 template <class T, class U, class B = ::boost::detail::empty_base<T> >
371 struct partially_ordered2 : B
373 friend bool operator<=(const T& x, const U& y)
374 { return static_cast<bool>(x < y) || static_cast<bool>(x == y); }
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 U& x, const T& y)
379 friend bool operator<(const U& x, const T& y)
381 friend bool operator<=(const U& x, const T& y)
382 { return static_cast<bool>(y > x) || static_cast<bool>(y == x); }
383 friend bool operator>=(const U& x, const T& y)
384 { return static_cast<bool>(y < x) || static_cast<bool>(y == x); }
387 template <class T, class B = ::boost::detail::empty_base<T> >
388 struct partially_ordered1 : B
390 friend bool operator>(const T& x, const T& y)
392 friend bool operator<=(const T& x, const T& y)
393 { return static_cast<bool>(x < y) || static_cast<bool>(x == y); }
394 friend bool operator>=(const T& x, const T& y)
395 { return static_cast<bool>(y < x) || static_cast<bool>(x == y); }
398 // Combined operator classes (contributed by Daryle Walker) ----------------//
400 template <class T, class U, class B = ::boost::detail::empty_base<T> >
401 struct totally_ordered2
402 : less_than_comparable2<T, U
403 , equality_comparable2<T, U, B
406 template <class T, class B = ::boost::detail::empty_base<T> >
407 struct totally_ordered1
408 : less_than_comparable1<T
409 , equality_comparable1<T, B
412 template <class T, class U, class B = ::boost::detail::empty_base<T> >
415 , subtractable2<T, U, B
418 template <class T, class B = ::boost::detail::empty_base<T> >
424 template <class T, class U, class B = ::boost::detail::empty_base<T> >
425 struct multiplicative2
430 template <class T, class B = ::boost::detail::empty_base<T> >
431 struct multiplicative1
436 template <class T, class U, class B = ::boost::detail::empty_base<T> >
437 struct integer_multiplicative2
438 : multiplicative2<T, U
442 template <class T, class B = ::boost::detail::empty_base<T> >
443 struct integer_multiplicative1
448 template <class T, class U, class B = ::boost::detail::empty_base<T> >
451 , multiplicative2<T, U, B
454 template <class T, class B = ::boost::detail::empty_base<T> >
457 , multiplicative1<T, B
460 template <class T, class U, class B = ::boost::detail::empty_base<T> >
461 struct integer_arithmetic2
463 , integer_multiplicative2<T, U, B
466 template <class T, class B = ::boost::detail::empty_base<T> >
467 struct integer_arithmetic1
469 , integer_multiplicative1<T, B
472 template <class T, class U, class B = ::boost::detail::empty_base<T> >
479 template <class T, class B = ::boost::detail::empty_base<T> >
486 template <class T, class B = ::boost::detail::empty_base<T> >
487 struct unit_steppable
492 template <class T, class U, class B = ::boost::detail::empty_base<T> >
494 : left_shiftable2<T, U
495 , right_shiftable2<T, U, B
498 template <class T, class B = ::boost::detail::empty_base<T> >
501 , right_shiftable1<T, B
504 template <class T, class U, class B = ::boost::detail::empty_base<T> >
505 struct ring_operators2
507 , subtractable2_left<T, U
508 , multipliable2<T, U, B
511 template <class T, class B = ::boost::detail::empty_base<T> >
512 struct ring_operators1
517 template <class T, class U, class B = ::boost::detail::empty_base<T> >
518 struct ordered_ring_operators2
519 : ring_operators2<T, U
520 , totally_ordered2<T, U, B
523 template <class T, class B = ::boost::detail::empty_base<T> >
524 struct ordered_ring_operators1
526 , totally_ordered1<T, B
529 template <class T, class U, class B = ::boost::detail::empty_base<T> >
530 struct field_operators2
531 : ring_operators2<T, U
533 , dividable2_left<T, U, B
536 template <class T, class B = ::boost::detail::empty_base<T> >
537 struct field_operators1
542 template <class T, class U, class B = ::boost::detail::empty_base<T> >
543 struct ordered_field_operators2
544 : field_operators2<T, U
545 , totally_ordered2<T, U, B
548 template <class T, class B = ::boost::detail::empty_base<T> >
549 struct ordered_field_operators1
551 , totally_ordered1<T, B
554 template <class T, class U, class B = ::boost::detail::empty_base<T> >
555 struct euclidian_ring_operators2
556 : ring_operators2<T, U
558 , dividable2_left<T, U
560 , modable2_left<T, U, B
563 template <class T, class B = ::boost::detail::empty_base<T> >
564 struct euclidian_ring_operators1
570 template <class T, class U, class B = ::boost::detail::empty_base<T> >
571 struct ordered_euclidian_ring_operators2
572 : totally_ordered2<T, U
573 , euclidian_ring_operators2<T, U, B
576 template <class T, class B = ::boost::detail::empty_base<T> >
577 struct ordered_euclidian_ring_operators1
579 , euclidian_ring_operators1<T, B
582 template <class T, class U, class B = ::boost::detail::empty_base<T> >
583 struct euclidean_ring_operators2
584 : ring_operators2<T, U
586 , dividable2_left<T, U
588 , modable2_left<T, U, B
591 template <class T, class B = ::boost::detail::empty_base<T> >
592 struct euclidean_ring_operators1
598 template <class T, class U, class B = ::boost::detail::empty_base<T> >
599 struct ordered_euclidean_ring_operators2
600 : totally_ordered2<T, U
601 , euclidean_ring_operators2<T, U, B
604 template <class T, class B = ::boost::detail::empty_base<T> >
605 struct ordered_euclidean_ring_operators1
607 , euclidean_ring_operators1<T, B
610 template <class T, class P, class B = ::boost::detail::empty_base<T> >
611 struct input_iteratable
612 : equality_comparable1<T
614 , dereferenceable<T, P, B
617 template <class T, class B = ::boost::detail::empty_base<T> >
618 struct output_iteratable
622 template <class T, class P, class B = ::boost::detail::empty_base<T> >
623 struct forward_iteratable
624 : input_iteratable<T, P, B
627 template <class T, class P, class B = ::boost::detail::empty_base<T> >
628 struct bidirectional_iteratable
629 : forward_iteratable<T, P
633 // To avoid repeated derivation from equality_comparable,
634 // which is an indirect base class of bidirectional_iterable,
635 // random_access_iteratable must not be derived from totally_ordered1
636 // but from less_than_comparable1 only. (Helmut Zeisel, 02-Dec-2001)
637 template <class T, class P, class D, class R, class B = ::boost::detail::empty_base<T> >
638 struct random_access_iteratable
639 : bidirectional_iteratable<T, P
640 , less_than_comparable1<T
642 , indexable<T, D, R, B
645 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
647 #endif // BOOST_NO_OPERATORS_IN_NAMESPACE
650 // BOOST_IMPORT_TEMPLATE1 .. BOOST_IMPORT_TEMPLATE4 -
652 // When BOOST_NO_OPERATORS_IN_NAMESPACE is defined we need a way to import an
653 // operator template into the boost namespace. BOOST_IMPORT_TEMPLATE1 is used
654 // for one-argument forms of operator templates; BOOST_IMPORT_TEMPLATE2 for
655 // two-argument forms. Note that these macros expect to be invoked from within
658 #ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
660 // The template is already in boost so we have nothing to do.
661 # define BOOST_IMPORT_TEMPLATE4(template_name)
662 # define BOOST_IMPORT_TEMPLATE3(template_name)
663 # define BOOST_IMPORT_TEMPLATE2(template_name)
664 # define BOOST_IMPORT_TEMPLATE1(template_name)
666 #else // BOOST_NO_OPERATORS_IN_NAMESPACE
668 # ifndef BOOST_NO_USING_TEMPLATE
670 // Bring the names in with a using-declaration
671 // to avoid stressing the compiler.
672 # define BOOST_IMPORT_TEMPLATE4(template_name) using ::template_name;
673 # define BOOST_IMPORT_TEMPLATE3(template_name) using ::template_name;
674 # define BOOST_IMPORT_TEMPLATE2(template_name) using ::template_name;
675 # define BOOST_IMPORT_TEMPLATE1(template_name) using ::template_name;
679 // Otherwise, because a Borland C++ 5.5 bug prevents a using declaration
680 // from working, we are forced to use inheritance for that compiler.
681 # define BOOST_IMPORT_TEMPLATE4(template_name) \
682 template <class T, class U, class V, class W, class B = ::boost::detail::empty_base<T> > \
683 struct template_name : ::template_name<T, U, V, W, B> {};
685 # define BOOST_IMPORT_TEMPLATE3(template_name) \
686 template <class T, class U, class V, class B = ::boost::detail::empty_base<T> > \
687 struct template_name : ::template_name<T, U, V, B> {};
689 # define BOOST_IMPORT_TEMPLATE2(template_name) \
690 template <class T, class U, class B = ::boost::detail::empty_base<T> > \
691 struct template_name : ::template_name<T, U, B> {};
693 # define BOOST_IMPORT_TEMPLATE1(template_name) \
694 template <class T, class B = ::boost::detail::empty_base<T> > \
695 struct template_name : ::template_name<T, B> {};
697 # endif // BOOST_NO_USING_TEMPLATE
699 #endif // BOOST_NO_OPERATORS_IN_NAMESPACE
702 // Here's where we put it all together, defining the xxxx forms of the templates
703 // in namespace boost. We also define specializations of is_chained_base<> for
704 // the xxxx, xxxx1, and xxxx2 templates, importing them into boost:: as
708 // is_chained_base<> - a traits class used to distinguish whether an operator
709 // template argument is being used for base class chaining, or is specifying a
710 // 2nd argument type.
713 // A type parameter is used instead of a plain bool because Borland's compiler
714 // didn't cope well with the more obvious non-type template parameter.
718 } // namespace detail
720 // Unspecialized version assumes that most types are not being used for base
721 // class chaining. We specialize for the operator templates defined in this
723 template<class T> struct is_chained_base {
724 typedef ::boost::detail::false_t value;
729 // Import a 4-type-argument operator template into boost (if necessary) and
730 // provide a specialization of 'is_chained_base<>' for it.
731 # define BOOST_OPERATOR_TEMPLATE4(template_name4) \
732 BOOST_IMPORT_TEMPLATE4(template_name4) \
733 template<class T, class U, class V, class W, class B> \
734 struct is_chained_base< ::boost::template_name4<T, U, V, W, B> > { \
735 typedef ::boost::detail::true_t value; \
738 // Import a 3-type-argument operator template into boost (if necessary) and
739 // provide a specialization of 'is_chained_base<>' for it.
740 # define BOOST_OPERATOR_TEMPLATE3(template_name3) \
741 BOOST_IMPORT_TEMPLATE3(template_name3) \
742 template<class T, class U, class V, class B> \
743 struct is_chained_base< ::boost::template_name3<T, U, V, B> > { \
744 typedef ::boost::detail::true_t value; \
747 // Import a 2-type-argument operator template into boost (if necessary) and
748 // provide a specialization of 'is_chained_base<>' for it.
749 # define BOOST_OPERATOR_TEMPLATE2(template_name2) \
750 BOOST_IMPORT_TEMPLATE2(template_name2) \
751 template<class T, class U, class B> \
752 struct is_chained_base< ::boost::template_name2<T, U, B> > { \
753 typedef ::boost::detail::true_t value; \
756 // Import a 1-type-argument operator template into boost (if necessary) and
757 // provide a specialization of 'is_chained_base<>' for it.
758 # define BOOST_OPERATOR_TEMPLATE1(template_name1) \
759 BOOST_IMPORT_TEMPLATE1(template_name1) \
760 template<class T, class B> \
761 struct is_chained_base< ::boost::template_name1<T, B> > { \
762 typedef ::boost::detail::true_t value; \
765 // BOOST_OPERATOR_TEMPLATE(template_name) defines template_name<> such that it
766 // can be used for specifying both 1-argument and 2-argument forms. Requires the
767 // existence of two previously defined class templates named '<template_name>1'
768 // and '<template_name>2' which must implement the corresponding 1- and 2-
771 // The template type parameter O == is_chained_base<U>::value is used to
772 // distinguish whether the 2nd argument to <template_name> is being used for
773 // base class chaining from another boost operator template or is describing a
774 // 2nd operand type. O == true_t only when U is actually an another operator
775 // template from the library. Partial specialization is used to select an
776 // implementation in terms of either '<template_name>1' or '<template_name>2'.
779 # define BOOST_OPERATOR_TEMPLATE(template_name) \
782 ,class B = ::boost::detail::empty_base<T> \
783 ,class O = typename is_chained_base<U>::value \
785 struct template_name : template_name##2<T, U, B> {}; \
787 template<class T, class U, class B> \
788 struct template_name<T, U, B, ::boost::detail::true_t> \
789 : template_name##1<T, U> {}; \
791 template <class T, class B> \
792 struct template_name<T, T, B, ::boost::detail::false_t> \
793 : template_name##1<T, B> {}; \
795 template<class T, class U, class B, class O> \
796 struct is_chained_base< ::boost::template_name<T, U, B, O> > { \
797 typedef ::boost::detail::true_t value; \
800 BOOST_OPERATOR_TEMPLATE2(template_name##2) \
801 BOOST_OPERATOR_TEMPLATE1(template_name##1)
807 BOOST_OPERATOR_TEMPLATE(less_than_comparable)
808 BOOST_OPERATOR_TEMPLATE(equality_comparable)
809 BOOST_OPERATOR_TEMPLATE(multipliable)
810 BOOST_OPERATOR_TEMPLATE(addable)
811 BOOST_OPERATOR_TEMPLATE(subtractable)
812 BOOST_OPERATOR_TEMPLATE2(subtractable2_left)
813 BOOST_OPERATOR_TEMPLATE(dividable)
814 BOOST_OPERATOR_TEMPLATE2(dividable2_left)
815 BOOST_OPERATOR_TEMPLATE(modable)
816 BOOST_OPERATOR_TEMPLATE2(modable2_left)
817 BOOST_OPERATOR_TEMPLATE(xorable)
818 BOOST_OPERATOR_TEMPLATE(andable)
819 BOOST_OPERATOR_TEMPLATE(orable)
821 BOOST_OPERATOR_TEMPLATE1(incrementable)
822 BOOST_OPERATOR_TEMPLATE1(decrementable)
824 BOOST_OPERATOR_TEMPLATE2(dereferenceable)
825 BOOST_OPERATOR_TEMPLATE3(indexable)
827 BOOST_OPERATOR_TEMPLATE(left_shiftable)
828 BOOST_OPERATOR_TEMPLATE(right_shiftable)
829 BOOST_OPERATOR_TEMPLATE(equivalent)
830 BOOST_OPERATOR_TEMPLATE(partially_ordered)
832 BOOST_OPERATOR_TEMPLATE(totally_ordered)
833 BOOST_OPERATOR_TEMPLATE(additive)
834 BOOST_OPERATOR_TEMPLATE(multiplicative)
835 BOOST_OPERATOR_TEMPLATE(integer_multiplicative)
836 BOOST_OPERATOR_TEMPLATE(arithmetic)
837 BOOST_OPERATOR_TEMPLATE(integer_arithmetic)
838 BOOST_OPERATOR_TEMPLATE(bitwise)
839 BOOST_OPERATOR_TEMPLATE1(unit_steppable)
840 BOOST_OPERATOR_TEMPLATE(shiftable)
841 BOOST_OPERATOR_TEMPLATE(ring_operators)
842 BOOST_OPERATOR_TEMPLATE(ordered_ring_operators)
843 BOOST_OPERATOR_TEMPLATE(field_operators)
844 BOOST_OPERATOR_TEMPLATE(ordered_field_operators)
845 BOOST_OPERATOR_TEMPLATE(euclidian_ring_operators)
846 BOOST_OPERATOR_TEMPLATE(ordered_euclidian_ring_operators)
847 BOOST_OPERATOR_TEMPLATE(euclidean_ring_operators)
848 BOOST_OPERATOR_TEMPLATE(ordered_euclidean_ring_operators)
849 BOOST_OPERATOR_TEMPLATE2(input_iteratable)
850 BOOST_OPERATOR_TEMPLATE1(output_iteratable)
851 BOOST_OPERATOR_TEMPLATE2(forward_iteratable)
852 BOOST_OPERATOR_TEMPLATE2(bidirectional_iteratable)
853 BOOST_OPERATOR_TEMPLATE4(random_access_iteratable)
855 #undef BOOST_OPERATOR_TEMPLATE
856 #undef BOOST_OPERATOR_TEMPLATE4
857 #undef BOOST_OPERATOR_TEMPLATE3
858 #undef BOOST_OPERATOR_TEMPLATE2
859 #undef BOOST_OPERATOR_TEMPLATE1
860 #undef BOOST_IMPORT_TEMPLATE1
861 #undef BOOST_IMPORT_TEMPLATE2
862 #undef BOOST_IMPORT_TEMPLATE3
863 #undef BOOST_IMPORT_TEMPLATE4
865 // The following 'operators' classes can only be used portably if the derived class
866 // declares ALL of the required member operators.
867 template <class T, class U>
869 : totally_ordered2<T,U
870 , integer_arithmetic2<T,U
874 template <class T, class U = T>
875 struct operators : operators2<T, U> {};
877 template <class T> struct operators<T, T>
879 , integer_arithmetic<T
884 // Iterator helper classes (contributed by Jeremy Siek) -------------------//
885 // (Input and output iterator helpers contributed by Daryle Walker) -------//
886 // (Changed to use combined operator classes by Daryle Walker) ------------//
889 class D = std::ptrdiff_t,
892 struct input_iterator_helper
893 : input_iteratable<T, P
894 , boost::iterator<std::input_iterator_tag, V, D, P, R
898 struct output_iterator_helper
899 : output_iteratable<T
900 , boost::iterator<std::output_iterator_tag, void, void, void, void
903 T& operator*() { return static_cast<T&>(*this); }
904 T& operator++() { return static_cast<T&>(*this); }
909 class D = std::ptrdiff_t,
912 struct forward_iterator_helper
913 : forward_iteratable<T, P
914 , boost::iterator<std::forward_iterator_tag, V, D, P, R
919 class D = std::ptrdiff_t,
922 struct bidirectional_iterator_helper
923 : bidirectional_iteratable<T, P
924 , boost::iterator<std::bidirectional_iterator_tag, V, D, P, R
929 class D = std::ptrdiff_t,
932 struct random_access_iterator_helper
933 : random_access_iteratable<T, P, D, R
934 , boost::iterator<std::random_access_iterator_tag, V, D, P, R
937 friend D requires_difference_operator(const T& x, const T& y) {
940 }; // random_access_iterator_helper
944 #if defined(__sgi) && !defined(__GNUC__)
945 #pragma reset woff 1234
948 #endif // BOOST_OPERATORS_HPP