1 ///////////////////////////////////////////////////////////////////////////////
2 // foreach.hpp header file
4 // Copyright 2004 Eric Niebler.
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org/libs/foreach for documentation
11 // Anson Tsao - for the initial inspiration and several good suggestions.
12 // Thorsten Ottosen - for Boost.Range, and for suggesting a way to detect
13 // const-qualified rvalues at compile time on VC7.1+
14 // Russell Hind - For help porting to Borland
15 // Alisdair Meredith - For help porting to Borland
16 // Stefan Slapeta - For help porting to Intel
17 // David Jenkins - For help finding a Microsoft Code Analysis bug
18 // mimomorin@... - For a patch to use rvalue refs on supporting compilers
22 // MS compatible compilers support #pragma once
28 #include <utility> // for std::pair
30 #include <boost/config.hpp>
31 #include <boost/detail/workaround.hpp>
33 // Some compilers let us detect even const-qualified rvalues at compile-time
34 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) \
35 || defined(BOOST_MSVC) && !defined(_PREFAST_) \
36 || (BOOST_WORKAROUND(__GNUC__, == 4) && (__GNUC_MINOR__ <= 5) && !defined(BOOST_INTEL) && \
37 !defined(BOOST_CLANG)) \
38 || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ >= 4) && !defined(BOOST_INTEL) && \
39 !defined(BOOST_CLANG))
40 # define BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION
42 // Some compilers allow temporaries to be bound to non-const references.
43 // These compilers make it impossible to for BOOST_FOREACH to detect
44 // temporaries and avoid reevaluation of the collection expression.
45 # if BOOST_WORKAROUND(__BORLANDC__, < 0x593) \
46 || (BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, <= 700) && defined(_MSC_VER)) \
47 || BOOST_WORKAROUND(__SUNPRO_CC, < 0x5100) \
48 || BOOST_WORKAROUND(__DECCXX_VER, <= 60590042)
49 # define BOOST_FOREACH_NO_RVALUE_DETECTION
51 // Some compilers do not correctly implement the lvalue/rvalue conversion
52 // rules of the ternary conditional operator.
53 # if defined(BOOST_FOREACH_NO_RVALUE_DETECTION) \
54 || defined(BOOST_NO_SFINAE) \
55 || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1400)) \
56 || BOOST_WORKAROUND(BOOST_INTEL_WIN, BOOST_TESTED_AT(1400)) \
57 || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ <= 3) && defined(__APPLE_CC__)) \
58 || BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600)) \
59 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206)) \
60 || BOOST_WORKAROUND(__SUNPRO_CC, >= 0x5100) \
61 || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x590))
62 # define BOOST_FOREACH_NO_CONST_RVALUE_DETECTION
64 # define BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
68 #include <boost/mpl/if.hpp>
69 #include <boost/mpl/assert.hpp>
70 #include <boost/mpl/logical.hpp>
71 #include <boost/mpl/eval_if.hpp>
72 #include <boost/noncopyable.hpp>
73 #include <boost/range/end.hpp>
74 #include <boost/range/begin.hpp>
75 #include <boost/range/rend.hpp>
76 #include <boost/range/rbegin.hpp>
77 #include <boost/range/iterator.hpp>
78 #include <boost/range/reverse_iterator.hpp>
79 #include <boost/type_traits/is_array.hpp>
80 #include <boost/type_traits/is_const.hpp>
81 #include <boost/type_traits/is_abstract.hpp>
82 #include <boost/type_traits/is_base_and_derived.hpp>
83 #include <boost/type_traits/is_rvalue_reference.hpp>
84 #include <boost/iterator/iterator_traits.hpp>
85 #include <boost/utility/addressof.hpp>
86 #include <boost/foreach_fwd.hpp>
88 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
90 # include <boost/aligned_storage.hpp>
91 # include <boost/utility/enable_if.hpp>
92 # include <boost/type_traits/remove_const.hpp>
98 // forward declarations for iterator_range
100 class iterator_range;
102 // forward declarations for sub_range
108 ///////////////////////////////////////////////////////////////////////////////
112 inline std::pair<T, T> in_range(T begin, T end)
114 return std::make_pair(begin, end);
117 ///////////////////////////////////////////////////////////////////////////////
118 // boost::foreach::is_lightweight_proxy
119 // Specialize this for user-defined collection types if they are inexpensive to copy.
120 // This tells BOOST_FOREACH it can avoid the rvalue/lvalue detection stuff.
122 struct is_lightweight_proxy
127 ///////////////////////////////////////////////////////////////////////////////
128 // boost::foreach::is_noncopyable
129 // Specialize this for user-defined collection types if they cannot be copied.
130 // This also tells BOOST_FOREACH to avoid the rvalue/lvalue detection stuff.
132 struct is_noncopyable
133 #if !defined(BOOST_BROKEN_IS_BASE_AND_DERIVED) && !defined(BOOST_NO_IS_ABSTRACT)
135 boost::is_abstract<T>
136 , boost::is_base_and_derived<boost::noncopyable, T>
138 #elif !defined(BOOST_BROKEN_IS_BASE_AND_DERIVED)
139 : boost::is_base_and_derived<boost::noncopyable, T>
140 #elif !defined(BOOST_NO_IS_ABSTRACT)
141 : boost::is_abstract<T>
148 } // namespace foreach
152 // vc6/7 needs help ordering the following overloads
153 #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
154 # define BOOST_FOREACH_TAG_DEFAULT ...
156 # define BOOST_FOREACH_TAG_DEFAULT boost::foreach::tag
159 ///////////////////////////////////////////////////////////////////////////////
160 // boost_foreach_is_lightweight_proxy
161 // Another customization point for the is_lightweight_proxy optimization,
162 // this one works on legacy compilers. Overload boost_foreach_is_lightweight_proxy
163 // at the global namespace for your type.
165 inline boost::foreach::is_lightweight_proxy<T> *
166 boost_foreach_is_lightweight_proxy(T *&, BOOST_FOREACH_TAG_DEFAULT) { return 0; }
169 inline boost::mpl::true_ *
170 boost_foreach_is_lightweight_proxy(std::pair<T, T> *&, boost::foreach::tag) { return 0; }
173 inline boost::mpl::true_ *
174 boost_foreach_is_lightweight_proxy(boost::iterator_range<T> *&, boost::foreach::tag) { return 0; }
177 inline boost::mpl::true_ *
178 boost_foreach_is_lightweight_proxy(boost::sub_range<T> *&, boost::foreach::tag) { return 0; }
181 inline boost::mpl::true_ *
182 boost_foreach_is_lightweight_proxy(T **&, boost::foreach::tag) { return 0; }
184 ///////////////////////////////////////////////////////////////////////////////
185 // boost_foreach_is_noncopyable
186 // Another customization point for the is_noncopyable trait,
187 // this one works on legacy compilers. Overload boost_foreach_is_noncopyable
188 // at the global namespace for your type.
190 inline boost::foreach::is_noncopyable<T> *
191 boost_foreach_is_noncopyable(T *&, BOOST_FOREACH_TAG_DEFAULT) { return 0; }
196 namespace foreach_detail_
199 ///////////////////////////////////////////////////////////////////////////////
200 // Define some utilities for assessing the properties of expressions
202 template<typename Bool1, typename Bool2>
203 inline boost::mpl::and_<Bool1, Bool2> *and_(Bool1 *, Bool2 *) { return 0; }
205 template<typename Bool1, typename Bool2, typename Bool3>
206 inline boost::mpl::and_<Bool1, Bool2, Bool3> *and_(Bool1 *, Bool2 *, Bool3 *) { return 0; }
208 template<typename Bool1, typename Bool2>
209 inline boost::mpl::or_<Bool1, Bool2> *or_(Bool1 *, Bool2 *) { return 0; }
211 template<typename Bool1, typename Bool2, typename Bool3>
212 inline boost::mpl::or_<Bool1, Bool2, Bool3> *or_(Bool1 *, Bool2 *, Bool3 *) { return 0; }
214 template<typename Bool1>
215 inline boost::mpl::not_<Bool1> *not_(Bool1 *) { return 0; }
218 inline boost::is_array<T> *is_array_(T const &) { return 0; }
221 inline boost::is_const<T> *is_const_(T &) { return 0; }
223 #ifndef BOOST_FOREACH_NO_RVALUE_DETECTION
225 inline boost::mpl::true_ *is_const_(T const &) { return 0; }
228 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
230 inline boost::mpl::false_ *is_rvalue_(T &, int) { return 0; }
233 inline boost::mpl::true_ *is_rvalue_(T const &, ...) { return 0; }
236 inline boost::is_rvalue_reference<T &&> *is_rvalue_(T &&, int) { return 0; }
239 ///////////////////////////////////////////////////////////////////////////////
240 // auto_any_t/auto_any
241 // General utility for putting an object of any type into automatic storage
244 // auto_any_base must evaluate to false in boolean context so that
245 // they can be declared in if() statements.
246 operator bool() const
253 struct auto_any : auto_any_base
255 explicit auto_any(T const &t)
260 // temporaries of type auto_any will be bound to const auto_any_base
261 // references, but we still want to be able to mutate the stored
262 // data, so declare it as mutable.
266 typedef auto_any_base const &auto_any_t;
268 template<typename T, typename C>
269 inline BOOST_DEDUCED_TYPENAME boost::mpl::if_<C, T const, T>::type &auto_any_cast(auto_any_t a)
271 return static_cast<auto_any<T> const &>(a).item;
274 typedef boost::mpl::true_ const_;
276 ///////////////////////////////////////////////////////////////////////////////
279 template<typename T, typename C = boost::mpl::false_>
281 : boost::mpl::if_<C, T const, T>
292 struct wrap_cstr<char *>
294 typedef wrap_cstr<char *> type;
295 typedef char *iterator;
296 typedef char *const_iterator;
300 struct wrap_cstr<char const *>
302 typedef wrap_cstr<char const *> type;
303 typedef char const *iterator;
304 typedef char const *const_iterator;
308 struct wrap_cstr<wchar_t *>
310 typedef wrap_cstr<wchar_t *> type;
311 typedef wchar_t *iterator;
312 typedef wchar_t *const_iterator;
316 struct wrap_cstr<wchar_t const *>
318 typedef wrap_cstr<wchar_t const *> type;
319 typedef wchar_t const *iterator;
320 typedef wchar_t const *const_iterator;
328 is_convertible<T, char const *>
329 , is_convertible<T, wchar_t const *>
334 template<typename T, typename C = boost::mpl::false_>
335 struct foreach_iterator
337 // **** READ THIS IF YOUR COMPILE BREAKS HERE ****
339 // There is an ambiguity about how to iterate over arrays of char and wchar_t.
340 // Should the last array element be treated as a null terminator to be skipped, or
341 // is it just like any other element in the array? To fix the problem, you must
342 // say which behavior you want.
344 // To treat the container as a null-terminated string, merely cast it to a
345 // char const *, as in BOOST_FOREACH( char ch, (char const *)"hello" ) ...
347 // To treat the container as an array, use boost::as_array() in <boost/range/as_array.hpp>,
348 // as in BOOST_FOREACH( char ch, boost::as_array("hello") ) ...
349 BOOST_MPL_ASSERT_MSG( (!is_char_array<T>::value), IS_THIS_AN_ARRAY_OR_A_NULL_TERMINATED_STRING, (T&) );
351 // If the type is a pointer to a null terminated string (as opposed
352 // to an array type), there is no ambiguity.
353 typedef BOOST_DEDUCED_TYPENAME wrap_cstr<T>::type container;
355 typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
357 , range_const_iterator<container>
358 , range_mutable_iterator<container>
363 template<typename T, typename C = boost::mpl::false_>
364 struct foreach_reverse_iterator
366 // **** READ THIS IF YOUR COMPILE BREAKS HERE ****
368 // There is an ambiguity about how to iterate over arrays of char and wchar_t.
369 // Should the last array element be treated as a null terminator to be skipped, or
370 // is it just like any other element in the array? To fix the problem, you must
371 // say which behavior you want.
373 // To treat the container as a null-terminated string, merely cast it to a
374 // char const *, as in BOOST_FOREACH( char ch, (char const *)"hello" ) ...
376 // To treat the container as an array, use boost::as_array() in <boost/range/as_array.hpp>,
377 // as in BOOST_FOREACH( char ch, boost::as_array("hello") ) ...
378 BOOST_MPL_ASSERT_MSG( (!is_char_array<T>::value), IS_THIS_AN_ARRAY_OR_A_NULL_TERMINATED_STRING, (T&) );
380 // If the type is a pointer to a null terminated string (as opposed
381 // to an array type), there is no ambiguity.
382 typedef BOOST_DEDUCED_TYPENAME wrap_cstr<T>::type container;
384 typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
386 , range_reverse_iterator<container const>
387 , range_reverse_iterator<container>
391 template<typename T, typename C = boost::mpl::false_>
392 struct foreach_reference
393 : iterator_reference<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
397 ///////////////////////////////////////////////////////////////////////////////
401 inline type2type<T> *encode_type(T &, boost::mpl::false_ *) { return 0; }
404 inline type2type<T, const_> *encode_type(T const &, boost::mpl::true_ *) { return 0; }
406 ///////////////////////////////////////////////////////////////////////////////
409 inline bool set_false(bool &b)
415 ///////////////////////////////////////////////////////////////////////////////
419 inline T *&to_ptr(T const &)
425 // Borland needs a little extra help with arrays
426 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
427 template<typename T,std::size_t N>
428 inline T (*&to_ptr(T (&)[N]))[N]
430 static T (*t)[N] = 0;
434 ///////////////////////////////////////////////////////////////////////////////
438 inline T &derefof(T *t)
440 // This is a work-around for a compiler bug in Borland. If T* is a pointer to array type U(*)[N],
441 // then dereferencing it results in a U* instead of U(&)[N]. The cast forces the issue.
442 return reinterpret_cast<T &>(
444 reinterpret_cast<char const volatile *>(t)
449 # define BOOST_FOREACH_DEREFOF(T) boost::foreach_detail_::derefof(*T)
451 # define BOOST_FOREACH_DEREFOF(T) (*T)
454 #if defined(BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION) \
455 && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
456 ///////////////////////////////////////////////////////////////////////////////
457 // Rvalue references makes it drop-dead simple to detect at compile time
458 // whether an expression is an rvalue.
459 ///////////////////////////////////////////////////////////////////////////////
461 # define BOOST_FOREACH_IS_RVALUE(COL) \
462 boost::foreach_detail_::is_rvalue_((COL), 0)
464 #elif defined(BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION) \
465 && defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
466 ///////////////////////////////////////////////////////////////////////////////
467 // Detect at compile-time whether an expression yields an rvalue or
468 // an lvalue. This is rather non-standard, but some popular compilers
470 ///////////////////////////////////////////////////////////////////////////////
472 ///////////////////////////////////////////////////////////////////////////////
478 struct private_type_ {};
479 // can't ever return an array by value
480 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
481 boost::mpl::or_<boost::is_abstract<T>, boost::is_array<T> >, private_type_, T
483 operator value_type() { return *reinterpret_cast<value_type *>(this); } // never called
484 operator T &() const { return *reinterpret_cast<T *>(const_cast<rvalue_probe *>(this)); } // never called
488 rvalue_probe<T> const make_probe(T const &)
490 return rvalue_probe<T>();
493 # define BOOST_FOREACH_IS_RVALUE(COL) \
494 boost::foreach_detail_::and_( \
495 boost::foreach_detail_::not_(boost::foreach_detail_::is_array_(COL)) \
496 , (true ? 0 : boost::foreach_detail_::is_rvalue_( \
497 (true ? boost::foreach_detail_::make_probe(COL) : (COL)), 0)))
499 #elif defined(BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION)
500 ///////////////////////////////////////////////////////////////////////////////
501 // Detect at run-time whether an expression yields an rvalue
502 // or an lvalue. This is 100% standard C++, but not all compilers
503 // accept it. Also, it causes FOREACH to break when used with non-
504 // copyable collection types.
505 ///////////////////////////////////////////////////////////////////////////////
507 ///////////////////////////////////////////////////////////////////////////////
513 rvalue_probe(T &t, bool &b)
519 struct private_type_ {};
520 // can't ever return an array or an abstract type by value
521 #ifdef BOOST_NO_IS_ABSTRACT
522 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
523 boost::is_array<T>, private_type_, T
526 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
527 boost::mpl::or_<boost::is_abstract<T>, boost::is_array<T> >, private_type_, T
531 operator value_type()
533 this->is_rvalue = true;
548 rvalue_probe<T> make_probe(T &t, bool &b) { return rvalue_probe<T>(t, b); }
551 rvalue_probe<T const> make_probe(T const &t, bool &b) { return rvalue_probe<T const>(t, b); }
553 ///////////////////////////////////////////////////////////////////////////////
555 // holds either a T or a T const*
557 struct simple_variant
559 simple_variant(T const *t)
562 *static_cast<T const **>(this->data.address()) = t;
565 simple_variant(T const &t)
568 ::new(this->data.address()) T(t);
571 simple_variant(simple_variant const &that)
572 : is_rvalue(that.is_rvalue)
575 ::new(this->data.address()) T(*that.get());
577 *static_cast<T const **>(this->data.address()) = that.get();
589 return static_cast<T const *>(this->data.address());
591 return *static_cast<T const * const *>(this->data.address());
595 enum size_type { size = sizeof(T) > sizeof(T*) ? sizeof(T) : sizeof(T*) };
596 simple_variant &operator =(simple_variant const &);
597 bool const is_rvalue;
598 aligned_storage<size> data;
601 // If the collection is an array or is noncopyable, it must be an lvalue.
602 // If the collection is a lightweight proxy, treat it as an rvalue
603 // BUGBUG what about a noncopyable proxy?
604 template<typename LValue, typename IsProxy>
605 inline BOOST_DEDUCED_TYPENAME boost::enable_if<boost::mpl::or_<LValue, IsProxy>, IsProxy>::type *
606 should_copy_impl(LValue *, IsProxy *, bool *)
611 // Otherwise, we must determine at runtime whether it's an lvalue or rvalue
613 should_copy_impl(boost::mpl::false_ *, boost::mpl::false_ *, bool *is_rvalue)
620 ///////////////////////////////////////////////////////////////////////////////
624 inline auto_any<T> contain(T const &t, boost::mpl::true_ *) // rvalue
626 return auto_any<T>(t);
630 inline auto_any<T *> contain(T &t, boost::mpl::false_ *) // lvalue
632 // Cannot seem to get sunpro to handle addressof() with array types.
633 #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x570))
634 return auto_any<T *>(&t);
636 return auto_any<T *>(boost::addressof(t));
640 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
642 inline auto_any<simple_variant<T> >
643 contain(T const &t, bool *rvalue)
645 return auto_any<simple_variant<T> >(*rvalue ? simple_variant<T>(t) : simple_variant<T>(&t));
649 /////////////////////////////////////////////////////////////////////////////
652 template<typename T, typename C>
653 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
654 begin(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
656 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
657 boost::begin(auto_any_cast<T, C>(col)));
660 template<typename T, typename C>
661 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
662 begin(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
664 typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
665 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iterator;
666 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
667 iterator(boost::begin(BOOST_FOREACH_DEREFOF((auto_any_cast<type *, boost::mpl::false_>(col))))));
670 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
672 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>
673 begin(auto_any_t col, type2type<T, const_> *, bool *)
675 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>(
676 boost::begin(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
680 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
681 template<typename T, typename C>
683 begin(auto_any_t col, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
685 return auto_any<T *>(auto_any_cast<T *, boost::mpl::false_>(col));
689 ///////////////////////////////////////////////////////////////////////////////
692 template<typename T, typename C>
693 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
694 end(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
696 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
697 boost::end(auto_any_cast<T, C>(col)));
700 template<typename T, typename C>
701 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
702 end(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
704 typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
705 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iterator;
706 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
707 iterator(boost::end(BOOST_FOREACH_DEREFOF((auto_any_cast<type *, boost::mpl::false_>(col))))));
710 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
712 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>
713 end(auto_any_t col, type2type<T, const_> *, bool *)
715 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>(
716 boost::end(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
720 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
721 template<typename T, typename C>
723 end(auto_any_t, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
725 return auto_any<int>(0); // not used
729 ///////////////////////////////////////////////////////////////////////////////
732 template<typename T, typename C>
733 inline bool done(auto_any_t cur, auto_any_t end, type2type<T, C> *)
735 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
736 return auto_any_cast<iter_t, boost::mpl::false_>(cur) == auto_any_cast<iter_t, boost::mpl::false_>(end);
739 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
740 template<typename T, typename C>
741 inline bool done(auto_any_t cur, auto_any_t, type2type<T *, C> *) // null-terminated C-style strings
743 return ! *auto_any_cast<T *, boost::mpl::false_>(cur);
747 ///////////////////////////////////////////////////////////////////////////////
750 template<typename T, typename C>
751 inline void next(auto_any_t cur, type2type<T, C> *)
753 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
754 ++auto_any_cast<iter_t, boost::mpl::false_>(cur);
757 ///////////////////////////////////////////////////////////////////////////////
760 template<typename T, typename C>
761 inline BOOST_DEDUCED_TYPENAME foreach_reference<T, C>::type
762 deref(auto_any_t cur, type2type<T, C> *)
764 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
765 return *auto_any_cast<iter_t, boost::mpl::false_>(cur);
768 /////////////////////////////////////////////////////////////////////////////
771 template<typename T, typename C>
772 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
773 rbegin(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
775 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
776 boost::rbegin(auto_any_cast<T, C>(col)));
779 template<typename T, typename C>
780 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
781 rbegin(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
783 typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
784 typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iterator;
785 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
786 iterator(boost::rbegin(BOOST_FOREACH_DEREFOF((auto_any_cast<type *, boost::mpl::false_>(col))))));
789 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
791 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>
792 rbegin(auto_any_t col, type2type<T, const_> *, bool *)
794 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>(
795 boost::rbegin(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
799 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
800 template<typename T, typename C>
801 inline auto_any<reverse_iterator<T *> >
802 rbegin(auto_any_t col, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
804 T *p = auto_any_cast<T *, boost::mpl::false_>(col);
807 return auto_any<reverse_iterator<T *> >(reverse_iterator<T *>(p));
811 ///////////////////////////////////////////////////////////////////////////////
814 template<typename T, typename C>
815 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
816 rend(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
818 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
819 boost::rend(auto_any_cast<T, C>(col)));
822 template<typename T, typename C>
823 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
824 rend(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
826 typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
827 typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iterator;
828 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
829 iterator(boost::rend(BOOST_FOREACH_DEREFOF((auto_any_cast<type *, boost::mpl::false_>(col))))));
832 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
834 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>
835 rend(auto_any_t col, type2type<T, const_> *, bool *)
837 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>(
838 boost::rend(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
842 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
843 template<typename T, typename C>
844 inline auto_any<reverse_iterator<T *> >
845 rend(auto_any_t col, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
847 return auto_any<reverse_iterator<T *> >(
848 reverse_iterator<T *>(auto_any_cast<T *, boost::mpl::false_>(col)));
852 ///////////////////////////////////////////////////////////////////////////////
855 template<typename T, typename C>
856 inline bool rdone(auto_any_t cur, auto_any_t end, type2type<T, C> *)
858 typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iter_t;
859 return auto_any_cast<iter_t, boost::mpl::false_>(cur) == auto_any_cast<iter_t, boost::mpl::false_>(end);
862 ///////////////////////////////////////////////////////////////////////////////
865 template<typename T, typename C>
866 inline void rnext(auto_any_t cur, type2type<T, C> *)
868 typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iter_t;
869 ++auto_any_cast<iter_t, boost::mpl::false_>(cur);
872 ///////////////////////////////////////////////////////////////////////////////
875 template<typename T, typename C>
876 inline BOOST_DEDUCED_TYPENAME foreach_reference<T, C>::type
877 rderef(auto_any_t cur, type2type<T, C> *)
879 typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iter_t;
880 return *auto_any_cast<iter_t, boost::mpl::false_>(cur);
883 } // namespace foreach_detail_
886 // Suppress a bogus code analysis warning on vc8+
887 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
888 # define BOOST_FOREACH_SUPPRESS_WARNINGS() __pragma(warning(suppress:6001))
890 # define BOOST_FOREACH_SUPPRESS_WARNINGS()
893 ///////////////////////////////////////////////////////////////////////////////
894 // Define a macro for giving hidden variables a unique name. Not strictly
895 // needed, but eliminates some warnings on some compilers.
896 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
897 // With some versions of MSVC, use of __LINE__ to create unique identifiers
898 // can fail when the Edit-and-Continue debug flag is used.
899 # define BOOST_FOREACH_ID(x) x
901 # define BOOST_FOREACH_ID(x) BOOST_PP_CAT(x, __LINE__)
904 // A sneaky way to get the type of the collection without evaluating the expression
905 #define BOOST_FOREACH_TYPEOF(COL) \
906 (true ? 0 : boost::foreach_detail_::encode_type(COL, boost::foreach_detail_::is_const_(COL)))
908 // returns true_* if the type is noncopyable
909 #define BOOST_FOREACH_IS_NONCOPYABLE(COL) \
910 boost_foreach_is_noncopyable( \
911 boost::foreach_detail_::to_ptr(COL) \
912 , boost_foreach_argument_dependent_lookup_hack_value)
914 // returns true_* if the type is a lightweight proxy (and is not noncopyable)
915 #define BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL) \
916 boost::foreach_detail_::and_( \
917 boost::foreach_detail_::not_(BOOST_FOREACH_IS_NONCOPYABLE(COL)) \
918 , boost_foreach_is_lightweight_proxy( \
919 boost::foreach_detail_::to_ptr(COL) \
920 , boost_foreach_argument_dependent_lookup_hack_value))
922 #if defined(BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION)
923 ///////////////////////////////////////////////////////////////////////////////
924 // R-values and const R-values supported here with zero runtime overhead
925 ///////////////////////////////////////////////////////////////////////////////
927 // No variable is needed to track the rvalue-ness of the collection expression
928 # define BOOST_FOREACH_PREAMBLE() \
929 BOOST_FOREACH_SUPPRESS_WARNINGS()
931 // Evaluate the collection expression
932 # define BOOST_FOREACH_EVALUATE(COL) \
935 # define BOOST_FOREACH_SHOULD_COPY(COL) \
936 (true ? 0 : boost::foreach_detail_::or_( \
937 BOOST_FOREACH_IS_RVALUE(COL) \
938 , BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL)))
940 #elif defined(BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION)
941 ///////////////////////////////////////////////////////////////////////////////
942 // R-values and const R-values supported here
943 ///////////////////////////////////////////////////////////////////////////////
945 // Declare a variable to track the rvalue-ness of the collection expression
946 # define BOOST_FOREACH_PREAMBLE() \
947 BOOST_FOREACH_SUPPRESS_WARNINGS() \
948 if (bool BOOST_FOREACH_ID(_foreach_is_rvalue) = false) {} else
950 // Evaluate the collection expression, and detect if it is an lvalue or and rvalue
951 # define BOOST_FOREACH_EVALUATE(COL) \
952 (true ? boost::foreach_detail_::make_probe((COL), BOOST_FOREACH_ID(_foreach_is_rvalue)) : (COL))
954 // The rvalue/lvalue-ness of the collection expression is determined dynamically, unless
955 // the type is an array or is noncopyable or is non-const, in which case we know it's an lvalue.
956 // If the type happens to be a lightweight proxy, always make a copy.
957 # define BOOST_FOREACH_SHOULD_COPY(COL) \
958 (boost::foreach_detail_::should_copy_impl( \
959 true ? 0 : boost::foreach_detail_::or_( \
960 boost::foreach_detail_::is_array_(COL) \
961 , BOOST_FOREACH_IS_NONCOPYABLE(COL) \
962 , boost::foreach_detail_::not_(boost::foreach_detail_::is_const_(COL))) \
963 , true ? 0 : BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL) \
964 , &BOOST_FOREACH_ID(_foreach_is_rvalue)))
966 #elif !defined(BOOST_FOREACH_NO_RVALUE_DETECTION)
967 ///////////////////////////////////////////////////////////////////////////////
968 // R-values supported here, const R-values NOT supported here
969 ///////////////////////////////////////////////////////////////////////////////
971 // No variable is needed to track the rvalue-ness of the collection expression
972 # define BOOST_FOREACH_PREAMBLE() \
973 BOOST_FOREACH_SUPPRESS_WARNINGS()
975 // Evaluate the collection expression
976 # define BOOST_FOREACH_EVALUATE(COL) \
979 // Determine whether the collection expression is an lvalue or an rvalue.
980 // NOTE: this gets the answer wrong for const rvalues.
981 # define BOOST_FOREACH_SHOULD_COPY(COL) \
982 (true ? 0 : boost::foreach_detail_::or_( \
983 boost::foreach_detail_::is_rvalue_((COL), 0) \
984 , BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL)))
987 ///////////////////////////////////////////////////////////////////////////////
988 // R-values NOT supported here
989 ///////////////////////////////////////////////////////////////////////////////
991 // No variable is needed to track the rvalue-ness of the collection expression
992 # define BOOST_FOREACH_PREAMBLE() \
993 BOOST_FOREACH_SUPPRESS_WARNINGS()
995 // Evaluate the collection expression
996 # define BOOST_FOREACH_EVALUATE(COL) \
999 // Can't use rvalues with BOOST_FOREACH (unless they are lightweight proxies)
1000 # define BOOST_FOREACH_SHOULD_COPY(COL) \
1001 (true ? 0 : BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL))
1005 #define BOOST_FOREACH_CONTAIN(COL) \
1006 boost::foreach_detail_::contain( \
1007 BOOST_FOREACH_EVALUATE(COL) \
1008 , BOOST_FOREACH_SHOULD_COPY(COL))
1010 #define BOOST_FOREACH_BEGIN(COL) \
1011 boost::foreach_detail_::begin( \
1012 BOOST_FOREACH_ID(_foreach_col) \
1013 , BOOST_FOREACH_TYPEOF(COL) \
1014 , BOOST_FOREACH_SHOULD_COPY(COL))
1016 #define BOOST_FOREACH_END(COL) \
1017 boost::foreach_detail_::end( \
1018 BOOST_FOREACH_ID(_foreach_col) \
1019 , BOOST_FOREACH_TYPEOF(COL) \
1020 , BOOST_FOREACH_SHOULD_COPY(COL))
1022 #define BOOST_FOREACH_DONE(COL) \
1023 boost::foreach_detail_::done( \
1024 BOOST_FOREACH_ID(_foreach_cur) \
1025 , BOOST_FOREACH_ID(_foreach_end) \
1026 , BOOST_FOREACH_TYPEOF(COL))
1028 #define BOOST_FOREACH_NEXT(COL) \
1029 boost::foreach_detail_::next( \
1030 BOOST_FOREACH_ID(_foreach_cur) \
1031 , BOOST_FOREACH_TYPEOF(COL))
1033 #define BOOST_FOREACH_DEREF(COL) \
1034 boost::foreach_detail_::deref( \
1035 BOOST_FOREACH_ID(_foreach_cur) \
1036 , BOOST_FOREACH_TYPEOF(COL))
1038 #define BOOST_FOREACH_RBEGIN(COL) \
1039 boost::foreach_detail_::rbegin( \
1040 BOOST_FOREACH_ID(_foreach_col) \
1041 , BOOST_FOREACH_TYPEOF(COL) \
1042 , BOOST_FOREACH_SHOULD_COPY(COL))
1044 #define BOOST_FOREACH_REND(COL) \
1045 boost::foreach_detail_::rend( \
1046 BOOST_FOREACH_ID(_foreach_col) \
1047 , BOOST_FOREACH_TYPEOF(COL) \
1048 , BOOST_FOREACH_SHOULD_COPY(COL))
1050 #define BOOST_FOREACH_RDONE(COL) \
1051 boost::foreach_detail_::rdone( \
1052 BOOST_FOREACH_ID(_foreach_cur) \
1053 , BOOST_FOREACH_ID(_foreach_end) \
1054 , BOOST_FOREACH_TYPEOF(COL))
1056 #define BOOST_FOREACH_RNEXT(COL) \
1057 boost::foreach_detail_::rnext( \
1058 BOOST_FOREACH_ID(_foreach_cur) \
1059 , BOOST_FOREACH_TYPEOF(COL))
1061 #define BOOST_FOREACH_RDEREF(COL) \
1062 boost::foreach_detail_::rderef( \
1063 BOOST_FOREACH_ID(_foreach_cur) \
1064 , BOOST_FOREACH_TYPEOF(COL))
1066 ///////////////////////////////////////////////////////////////////////////////
1069 // For iterating over collections. Collections can be
1070 // arrays, null-terminated strings, or STL containers.
1071 // The loop variable can be a value or reference. For
1074 // std::list<int> int_list(/*stuff*/);
1075 // BOOST_FOREACH(int &i, int_list)
1078 // * loop body goes here.
1079 // * i is a reference to the int in int_list.
1083 // Alternately, you can declare the loop variable first,
1084 // so you can access it after the loop finishes. Obviously,
1085 // if you do it this way, then the loop variable cannot be
1089 // BOOST_FOREACH(i, int_list)
1092 #define BOOST_FOREACH(VAR, COL) \
1093 BOOST_FOREACH_PREAMBLE() \
1094 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_col) = BOOST_FOREACH_CONTAIN(COL)) {} else \
1095 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_cur) = BOOST_FOREACH_BEGIN(COL)) {} else \
1096 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_end) = BOOST_FOREACH_END(COL)) {} else \
1097 for (bool BOOST_FOREACH_ID(_foreach_continue) = true; \
1098 BOOST_FOREACH_ID(_foreach_continue) && !BOOST_FOREACH_DONE(COL); \
1099 BOOST_FOREACH_ID(_foreach_continue) ? BOOST_FOREACH_NEXT(COL) : (void)0) \
1100 if (boost::foreach_detail_::set_false(BOOST_FOREACH_ID(_foreach_continue))) {} else \
1101 for (VAR = BOOST_FOREACH_DEREF(COL); !BOOST_FOREACH_ID(_foreach_continue); BOOST_FOREACH_ID(_foreach_continue) = true)
1103 ///////////////////////////////////////////////////////////////////////////////
1104 // BOOST_REVERSE_FOREACH
1106 // For iterating over collections in reverse order. In
1107 // all other respects, BOOST_REVERSE_FOREACH is like
1110 #define BOOST_REVERSE_FOREACH(VAR, COL) \
1111 BOOST_FOREACH_PREAMBLE() \
1112 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_col) = BOOST_FOREACH_CONTAIN(COL)) {} else \
1113 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_cur) = BOOST_FOREACH_RBEGIN(COL)) {} else \
1114 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_end) = BOOST_FOREACH_REND(COL)) {} else \
1115 for (bool BOOST_FOREACH_ID(_foreach_continue) = true; \
1116 BOOST_FOREACH_ID(_foreach_continue) && !BOOST_FOREACH_RDONE(COL); \
1117 BOOST_FOREACH_ID(_foreach_continue) ? BOOST_FOREACH_RNEXT(COL) : (void)0) \
1118 if (boost::foreach_detail_::set_false(BOOST_FOREACH_ID(_foreach_continue))) {} else \
1119 for (VAR = BOOST_FOREACH_RDEREF(COL); !BOOST_FOREACH_ID(_foreach_continue); BOOST_FOREACH_ID(_foreach_continue) = true)