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
23 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
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_RVALUE_REFERENCES) \
35 || BOOST_WORKAROUND(BOOST_MSVC, >= 1310) && !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(BOOST_MSVC, <= 1300) \
46 || BOOST_WORKAROUND(__BORLANDC__, < 0x593) \
47 || (BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, <= 700) && defined(_MSC_VER)) \
48 || BOOST_WORKAROUND(__SUNPRO_CC, < 0x5100) \
49 || BOOST_WORKAROUND(__DECCXX_VER, <= 60590042)
50 # define BOOST_FOREACH_NO_RVALUE_DETECTION
52 // Some compilers do not correctly implement the lvalue/rvalue conversion
53 // rules of the ternary conditional operator.
54 # if defined(BOOST_FOREACH_NO_RVALUE_DETECTION) \
55 || defined(BOOST_NO_SFINAE) \
56 || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1400)) \
57 || BOOST_WORKAROUND(BOOST_INTEL_WIN, BOOST_TESTED_AT(1400)) \
58 || BOOST_WORKAROUND(__GNUC__, < 3) \
59 || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ <= 2)) \
60 || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ <= 3) && defined(__APPLE_CC__)) \
61 || BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600)) \
62 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206)) \
63 || BOOST_WORKAROUND(__SUNPRO_CC, >= 0x5100) \
64 || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x590))
65 # define BOOST_FOREACH_NO_CONST_RVALUE_DETECTION
67 # define BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
71 #include <boost/mpl/if.hpp>
72 #include <boost/mpl/assert.hpp>
73 #include <boost/mpl/logical.hpp>
74 #include <boost/mpl/eval_if.hpp>
75 #include <boost/noncopyable.hpp>
76 #include <boost/range/end.hpp>
77 #include <boost/range/begin.hpp>
78 #include <boost/range/rend.hpp>
79 #include <boost/range/rbegin.hpp>
80 #include <boost/range/iterator.hpp>
81 #include <boost/range/reverse_iterator.hpp>
82 #include <boost/type_traits/is_array.hpp>
83 #include <boost/type_traits/is_const.hpp>
84 #include <boost/type_traits/is_abstract.hpp>
85 #include <boost/type_traits/is_base_and_derived.hpp>
86 #include <boost/type_traits/is_rvalue_reference.hpp>
87 #include <boost/iterator/iterator_traits.hpp>
88 #include <boost/utility/addressof.hpp>
89 #include <boost/foreach_fwd.hpp>
91 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
93 # include <boost/aligned_storage.hpp>
94 # include <boost/utility/enable_if.hpp>
95 # include <boost/type_traits/remove_const.hpp>
101 // forward declarations for iterator_range
103 class iterator_range;
105 // forward declarations for sub_range
111 ///////////////////////////////////////////////////////////////////////////////
115 inline std::pair<T, T> in_range(T begin, T end)
117 return std::make_pair(begin, end);
120 ///////////////////////////////////////////////////////////////////////////////
121 // boost::foreach::is_lightweight_proxy
122 // Specialize this for user-defined collection types if they are inexpensive to copy.
123 // This tells BOOST_FOREACH it can avoid the rvalue/lvalue detection stuff.
125 struct is_lightweight_proxy
130 ///////////////////////////////////////////////////////////////////////////////
131 // boost::foreach::is_noncopyable
132 // Specialize this for user-defined collection types if they cannot be copied.
133 // This also tells BOOST_FOREACH to avoid the rvalue/lvalue detection stuff.
135 struct is_noncopyable
136 #if !defined(BOOST_BROKEN_IS_BASE_AND_DERIVED) && !defined(BOOST_NO_IS_ABSTRACT)
138 boost::is_abstract<T>
139 , boost::is_base_and_derived<boost::noncopyable, T>
141 #elif !defined(BOOST_BROKEN_IS_BASE_AND_DERIVED)
142 : boost::is_base_and_derived<boost::noncopyable, T>
143 #elif !defined(BOOST_NO_IS_ABSTRACT)
144 : boost::is_abstract<T>
151 } // namespace foreach
155 // vc6/7 needs help ordering the following overloads
156 #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
157 # define BOOST_FOREACH_TAG_DEFAULT ...
159 # define BOOST_FOREACH_TAG_DEFAULT boost::foreach::tag
162 ///////////////////////////////////////////////////////////////////////////////
163 // boost_foreach_is_lightweight_proxy
164 // Another customization point for the is_lightweight_proxy optimization,
165 // this one works on legacy compilers. Overload boost_foreach_is_lightweight_proxy
166 // at the global namespace for your type.
168 inline boost::foreach::is_lightweight_proxy<T> *
169 boost_foreach_is_lightweight_proxy(T *&, BOOST_FOREACH_TAG_DEFAULT) { return 0; }
172 inline boost::mpl::true_ *
173 boost_foreach_is_lightweight_proxy(std::pair<T, T> *&, boost::foreach::tag) { return 0; }
176 inline boost::mpl::true_ *
177 boost_foreach_is_lightweight_proxy(boost::iterator_range<T> *&, boost::foreach::tag) { return 0; }
180 inline boost::mpl::true_ *
181 boost_foreach_is_lightweight_proxy(boost::sub_range<T> *&, boost::foreach::tag) { return 0; }
184 inline boost::mpl::true_ *
185 boost_foreach_is_lightweight_proxy(T **&, boost::foreach::tag) { return 0; }
187 ///////////////////////////////////////////////////////////////////////////////
188 // boost_foreach_is_noncopyable
189 // Another customization point for the is_noncopyable trait,
190 // this one works on legacy compilers. Overload boost_foreach_is_noncopyable
191 // at the global namespace for your type.
193 inline boost::foreach::is_noncopyable<T> *
194 boost_foreach_is_noncopyable(T *&, BOOST_FOREACH_TAG_DEFAULT) { return 0; }
199 namespace foreach_detail_
202 ///////////////////////////////////////////////////////////////////////////////
203 // Define some utilities for assessing the properties of expressions
205 template<typename Bool1, typename Bool2>
206 inline boost::mpl::and_<Bool1, Bool2> *and_(Bool1 *, Bool2 *) { return 0; }
208 template<typename Bool1, typename Bool2, typename Bool3>
209 inline boost::mpl::and_<Bool1, Bool2, Bool3> *and_(Bool1 *, Bool2 *, Bool3 *) { return 0; }
211 template<typename Bool1, typename Bool2>
212 inline boost::mpl::or_<Bool1, Bool2> *or_(Bool1 *, Bool2 *) { return 0; }
214 template<typename Bool1, typename Bool2, typename Bool3>
215 inline boost::mpl::or_<Bool1, Bool2, Bool3> *or_(Bool1 *, Bool2 *, Bool3 *) { return 0; }
217 template<typename Bool1>
218 inline boost::mpl::not_<Bool1> *not_(Bool1 *) { return 0; }
221 inline boost::is_array<T> *is_array_(T const &) { return 0; }
224 inline boost::is_const<T> *is_const_(T &) { return 0; }
226 #ifndef BOOST_FOREACH_NO_RVALUE_DETECTION
228 inline boost::mpl::true_ *is_const_(T const &) { return 0; }
231 #ifdef BOOST_NO_RVALUE_REFERENCES
233 inline boost::mpl::false_ *is_rvalue_(T &, int) { return 0; }
236 inline boost::mpl::true_ *is_rvalue_(T const &, ...) { return 0; }
239 inline boost::is_rvalue_reference<T &&> *is_rvalue_(T &&, int) { return 0; }
242 ///////////////////////////////////////////////////////////////////////////////
243 // auto_any_t/auto_any
244 // General utility for putting an object of any type into automatic storage
247 // auto_any_base must evaluate to false in boolean context so that
248 // they can be declared in if() statements.
249 operator bool() const
256 struct auto_any : auto_any_base
258 explicit auto_any(T const &t)
263 // temporaries of type auto_any will be bound to const auto_any_base
264 // references, but we still want to be able to mutate the stored
265 // data, so declare it as mutable.
269 typedef auto_any_base const &auto_any_t;
271 template<typename T, typename C>
272 inline BOOST_DEDUCED_TYPENAME boost::mpl::if_<C, T const, T>::type &auto_any_cast(auto_any_t a)
274 return static_cast<auto_any<T> const &>(a).item;
277 typedef boost::mpl::true_ const_;
279 ///////////////////////////////////////////////////////////////////////////////
282 template<typename T, typename C = boost::mpl::false_>
284 : boost::mpl::if_<C, T const, T>
295 struct wrap_cstr<char *>
297 typedef wrap_cstr<char *> type;
298 typedef char *iterator;
299 typedef char *const_iterator;
303 struct wrap_cstr<char const *>
305 typedef wrap_cstr<char const *> type;
306 typedef char const *iterator;
307 typedef char const *const_iterator;
311 struct wrap_cstr<wchar_t *>
313 typedef wrap_cstr<wchar_t *> type;
314 typedef wchar_t *iterator;
315 typedef wchar_t *const_iterator;
319 struct wrap_cstr<wchar_t const *>
321 typedef wrap_cstr<wchar_t const *> type;
322 typedef wchar_t const *iterator;
323 typedef wchar_t const *const_iterator;
331 is_convertible<T, char const *>
332 , is_convertible<T, wchar_t const *>
337 template<typename T, typename C = boost::mpl::false_>
338 struct foreach_iterator
340 // **** READ THIS IF YOUR COMPILE BREAKS HERE ****
342 // There is an ambiguity about how to iterate over arrays of char and wchar_t.
343 // Should the last array element be treated as a null terminator to be skipped, or
344 // is it just like any other element in the array? To fix the problem, you must
345 // say which behavior you want.
347 // To treat the container as a null-terminated string, merely cast it to a
348 // char const *, as in BOOST_FOREACH( char ch, (char const *)"hello" ) ...
350 // To treat the container as an array, use boost::as_array() in <boost/range/as_array.hpp>,
351 // as in BOOST_FOREACH( char ch, boost::as_array("hello") ) ...
352 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
353 BOOST_MPL_ASSERT_MSG( (!is_char_array<T>::value), IS_THIS_AN_ARRAY_OR_A_NULL_TERMINATED_STRING, (T&) );
356 // If the type is a pointer to a null terminated string (as opposed
357 // to an array type), there is no ambiguity.
358 typedef BOOST_DEDUCED_TYPENAME wrap_cstr<T>::type container;
360 typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
362 , range_const_iterator<container>
363 , range_mutable_iterator<container>
368 template<typename T, typename C = boost::mpl::false_>
369 struct foreach_reverse_iterator
371 // **** READ THIS IF YOUR COMPILE BREAKS HERE ****
373 // There is an ambiguity about how to iterate over arrays of char and wchar_t.
374 // Should the last array element be treated as a null terminator to be skipped, or
375 // is it just like any other element in the array? To fix the problem, you must
376 // say which behavior you want.
378 // To treat the container as a null-terminated string, merely cast it to a
379 // char const *, as in BOOST_FOREACH( char ch, (char const *)"hello" ) ...
381 // To treat the container as an array, use boost::as_array() in <boost/range/as_array.hpp>,
382 // as in BOOST_FOREACH( char ch, boost::as_array("hello") ) ...
383 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
384 BOOST_MPL_ASSERT_MSG( (!is_char_array<T>::value), IS_THIS_AN_ARRAY_OR_A_NULL_TERMINATED_STRING, (T&) );
387 // If the type is a pointer to a null terminated string (as opposed
388 // to an array type), there is no ambiguity.
389 typedef BOOST_DEDUCED_TYPENAME wrap_cstr<T>::type container;
391 typedef BOOST_DEDUCED_TYPENAME boost::mpl::eval_if<
393 , range_reverse_iterator<container const>
394 , range_reverse_iterator<container>
398 template<typename T, typename C = boost::mpl::false_>
399 struct foreach_reference
400 : iterator_reference<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
404 ///////////////////////////////////////////////////////////////////////////////
408 inline type2type<T> *encode_type(T &, boost::mpl::false_ *) { return 0; }
411 inline type2type<T, const_> *encode_type(T const &, boost::mpl::true_ *) { return 0; }
413 ///////////////////////////////////////////////////////////////////////////////
416 inline bool set_false(bool &b)
422 ///////////////////////////////////////////////////////////////////////////////
426 inline T *&to_ptr(T const &)
432 // Borland needs a little extra help with arrays
433 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
434 template<typename T,std::size_t N>
435 inline T (*&to_ptr(T (&)[N]))[N]
437 static T (*t)[N] = 0;
442 ///////////////////////////////////////////////////////////////////////////////
446 inline T &derefof(T *t)
448 // This is a work-around for a compiler bug in Borland. If T* is a pointer to array type U(*)[N],
449 // then dereferencing it results in a U* instead of U(&)[N]. The cast forces the issue.
450 return reinterpret_cast<T &>(
452 reinterpret_cast<char const volatile *>(t)
457 #if defined(BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION) \
458 && !defined(BOOST_NO_RVALUE_REFERENCES)
459 ///////////////////////////////////////////////////////////////////////////////
460 // Rvalue references makes it drop-dead simple to detect at compile time
461 // whether an expression is an rvalue.
462 ///////////////////////////////////////////////////////////////////////////////
464 # define BOOST_FOREACH_IS_RVALUE(COL) \
465 boost::foreach_detail_::is_rvalue_((COL), 0)
467 #elif defined(BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION) \
468 && defined(BOOST_NO_RVALUE_REFERENCES)
469 ///////////////////////////////////////////////////////////////////////////////
470 // Detect at compile-time whether an expression yields an rvalue or
471 // an lvalue. This is rather non-standard, but some popular compilers
473 ///////////////////////////////////////////////////////////////////////////////
475 ///////////////////////////////////////////////////////////////////////////////
481 struct private_type_ {};
482 // can't ever return an array by value
483 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
484 boost::mpl::or_<boost::is_abstract<T>, boost::is_array<T> >, private_type_, T
486 operator value_type() { return *reinterpret_cast<value_type *>(this); } // never called
487 operator T &() const { return *reinterpret_cast<T *>(const_cast<rvalue_probe *>(this)); } // never called
491 rvalue_probe<T> const make_probe(T const &)
493 return rvalue_probe<T>();
496 # define BOOST_FOREACH_IS_RVALUE(COL) \
497 boost::foreach_detail_::and_( \
498 boost::foreach_detail_::not_(boost::foreach_detail_::is_array_(COL)) \
499 , (true ? 0 : boost::foreach_detail_::is_rvalue_( \
500 (true ? boost::foreach_detail_::make_probe(COL) : (COL)), 0)))
502 #elif defined(BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION)
503 ///////////////////////////////////////////////////////////////////////////////
504 // Detect at run-time whether an expression yields an rvalue
505 // or an lvalue. This is 100% standard C++, but not all compilers
506 // accept it. Also, it causes FOREACH to break when used with non-
507 // copyable collection types.
508 ///////////////////////////////////////////////////////////////////////////////
510 ///////////////////////////////////////////////////////////////////////////////
516 rvalue_probe(T &t, bool &b)
522 struct private_type_ {};
523 // can't ever return an array or an abstract type by value
524 #ifdef BOOST_NO_IS_ABSTRACT
525 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
526 boost::is_array<T>, private_type_, T
529 typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
530 boost::mpl::or_<boost::is_abstract<T>, boost::is_array<T> >, private_type_, T
534 operator value_type()
536 this->is_rvalue = true;
551 rvalue_probe<T> make_probe(T &t, bool &b) { return rvalue_probe<T>(t, b); }
554 rvalue_probe<T const> make_probe(T const &t, bool &b) { return rvalue_probe<T const>(t, b); }
556 ///////////////////////////////////////////////////////////////////////////////
558 // holds either a T or a T const*
560 struct simple_variant
562 simple_variant(T const *t)
565 *static_cast<T const **>(this->data.address()) = t;
568 simple_variant(T const &t)
571 ::new(this->data.address()) T(t);
574 simple_variant(simple_variant const &that)
575 : is_rvalue(that.is_rvalue)
578 ::new(this->data.address()) T(*that.get());
580 *static_cast<T const **>(this->data.address()) = that.get();
592 return static_cast<T const *>(this->data.address());
594 return *static_cast<T const * const *>(this->data.address());
598 enum size_type { size = sizeof(T) > sizeof(T*) ? sizeof(T) : sizeof(T*) };
599 simple_variant &operator =(simple_variant const &);
600 bool const is_rvalue;
601 aligned_storage<size> data;
604 // If the collection is an array or is noncopyable, it must be an lvalue.
605 // If the collection is a lightweight proxy, treat it as an rvalue
606 // BUGBUG what about a noncopyable proxy?
607 template<typename LValue, typename IsProxy>
608 inline BOOST_DEDUCED_TYPENAME boost::enable_if<boost::mpl::or_<LValue, IsProxy>, IsProxy>::type *
609 should_copy_impl(LValue *, IsProxy *, bool *)
614 // Otherwise, we must determine at runtime whether it's an lvalue or rvalue
616 should_copy_impl(boost::mpl::false_ *, boost::mpl::false_ *, bool *is_rvalue)
623 ///////////////////////////////////////////////////////////////////////////////
627 inline auto_any<T> contain(T const &t, boost::mpl::true_ *) // rvalue
629 return auto_any<T>(t);
633 inline auto_any<T *> contain(T &t, boost::mpl::false_ *) // lvalue
635 // Cannot seem to get sunpro to handle addressof() with array types.
636 #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x570))
637 return auto_any<T *>(&t);
639 return auto_any<T *>(boost::addressof(t));
643 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
645 inline auto_any<simple_variant<T> >
646 contain(T const &t, bool *rvalue)
648 return auto_any<simple_variant<T> >(*rvalue ? simple_variant<T>(t) : simple_variant<T>(&t));
652 /////////////////////////////////////////////////////////////////////////////
655 template<typename T, typename C>
656 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
657 begin(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
659 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
660 boost::begin(auto_any_cast<T, C>(col)));
663 template<typename T, typename C>
664 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
665 begin(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
667 typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
668 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iterator;
669 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
670 iterator(boost::begin(derefof(auto_any_cast<type *, boost::mpl::false_>(col)))));
673 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
675 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>
676 begin(auto_any_t col, type2type<T, const_> *, bool *)
678 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>(
679 boost::begin(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
683 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
684 template<typename T, typename C>
686 begin(auto_any_t col, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
688 return auto_any<T *>(auto_any_cast<T *, boost::mpl::false_>(col));
692 ///////////////////////////////////////////////////////////////////////////////
695 template<typename T, typename C>
696 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
697 end(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
699 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
700 boost::end(auto_any_cast<T, C>(col)));
703 template<typename T, typename C>
704 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>
705 end(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
707 typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
708 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iterator;
709 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type>(
710 iterator(boost::end(derefof(auto_any_cast<type *, boost::mpl::false_>(col)))));
713 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
715 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>
716 end(auto_any_t col, type2type<T, const_> *, bool *)
718 return auto_any<BOOST_DEDUCED_TYPENAME foreach_iterator<T, const_>::type>(
719 boost::end(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
723 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
724 template<typename T, typename C>
726 end(auto_any_t, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
728 return auto_any<int>(0); // not used
732 ///////////////////////////////////////////////////////////////////////////////
735 template<typename T, typename C>
736 inline bool done(auto_any_t cur, auto_any_t end, type2type<T, C> *)
738 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
739 return auto_any_cast<iter_t, boost::mpl::false_>(cur) == auto_any_cast<iter_t, boost::mpl::false_>(end);
742 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
743 template<typename T, typename C>
744 inline bool done(auto_any_t cur, auto_any_t, type2type<T *, C> *) // null-terminated C-style strings
746 return ! *auto_any_cast<T *, boost::mpl::false_>(cur);
750 ///////////////////////////////////////////////////////////////////////////////
753 template<typename T, typename C>
754 inline void next(auto_any_t cur, type2type<T, C> *)
756 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
757 ++auto_any_cast<iter_t, boost::mpl::false_>(cur);
760 ///////////////////////////////////////////////////////////////////////////////
763 template<typename T, typename C>
764 inline BOOST_DEDUCED_TYPENAME foreach_reference<T, C>::type
765 deref(auto_any_t cur, type2type<T, C> *)
767 typedef BOOST_DEDUCED_TYPENAME foreach_iterator<T, C>::type iter_t;
768 return *auto_any_cast<iter_t, boost::mpl::false_>(cur);
771 /////////////////////////////////////////////////////////////////////////////
774 template<typename T, typename C>
775 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
776 rbegin(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
778 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
779 boost::rbegin(auto_any_cast<T, C>(col)));
782 template<typename T, typename C>
783 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
784 rbegin(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
786 typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
787 typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iterator;
788 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
789 iterator(boost::rbegin(derefof(auto_any_cast<type *, boost::mpl::false_>(col)))));
792 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
794 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>
795 rbegin(auto_any_t col, type2type<T, const_> *, bool *)
797 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>(
798 boost::rbegin(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
802 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
803 template<typename T, typename C>
804 inline auto_any<reverse_iterator<T *> >
805 rbegin(auto_any_t col, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
807 T *p = auto_any_cast<T *, boost::mpl::false_>(col);
810 return auto_any<reverse_iterator<T *> >(reverse_iterator<T *>(p));
814 ///////////////////////////////////////////////////////////////////////////////
817 template<typename T, typename C>
818 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
819 rend(auto_any_t col, type2type<T, C> *, boost::mpl::true_ *) // rvalue
821 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
822 boost::rend(auto_any_cast<T, C>(col)));
825 template<typename T, typename C>
826 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>
827 rend(auto_any_t col, type2type<T, C> *, boost::mpl::false_ *) // lvalue
829 typedef BOOST_DEDUCED_TYPENAME type2type<T, C>::type type;
830 typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iterator;
831 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type>(
832 iterator(boost::rend(derefof(auto_any_cast<type *, boost::mpl::false_>(col)))));
835 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
837 inline auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>
838 rend(auto_any_t col, type2type<T, const_> *, bool *)
840 return auto_any<BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, const_>::type>(
841 boost::rend(*auto_any_cast<simple_variant<T>, boost::mpl::false_>(col).get()));
845 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
846 template<typename T, typename C>
847 inline auto_any<reverse_iterator<T *> >
848 rend(auto_any_t col, type2type<T *, C> *, boost::mpl::true_ *) // null-terminated C-style strings
850 return auto_any<reverse_iterator<T *> >(
851 reverse_iterator<T *>(auto_any_cast<T *, boost::mpl::false_>(col)));
855 ///////////////////////////////////////////////////////////////////////////////
858 template<typename T, typename C>
859 inline bool rdone(auto_any_t cur, auto_any_t end, type2type<T, C> *)
861 typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iter_t;
862 return auto_any_cast<iter_t, boost::mpl::false_>(cur) == auto_any_cast<iter_t, boost::mpl::false_>(end);
865 ///////////////////////////////////////////////////////////////////////////////
868 template<typename T, typename C>
869 inline void rnext(auto_any_t cur, type2type<T, C> *)
871 typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iter_t;
872 ++auto_any_cast<iter_t, boost::mpl::false_>(cur);
875 ///////////////////////////////////////////////////////////////////////////////
878 template<typename T, typename C>
879 inline BOOST_DEDUCED_TYPENAME foreach_reference<T, C>::type
880 rderef(auto_any_t cur, type2type<T, C> *)
882 typedef BOOST_DEDUCED_TYPENAME foreach_reverse_iterator<T, C>::type iter_t;
883 return *auto_any_cast<iter_t, boost::mpl::false_>(cur);
886 } // namespace foreach_detail_
889 // Suppress a bogus code analysis warning on vc8+
890 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
891 # define BOOST_FOREACH_SUPPRESS_WARNINGS() __pragma(warning(suppress:6001))
893 # define BOOST_FOREACH_SUPPRESS_WARNINGS()
896 ///////////////////////////////////////////////////////////////////////////////
897 // Define a macro for giving hidden variables a unique name. Not strictly
898 // needed, but eliminates some warnings on some compilers.
899 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
900 // With some versions of MSVC, use of __LINE__ to create unique identifiers
901 // can fail when the Edit-and-Continue debug flag is used.
902 # define BOOST_FOREACH_ID(x) x
904 # define BOOST_FOREACH_ID(x) BOOST_PP_CAT(x, __LINE__)
907 // A sneaky way to get the type of the collection without evaluating the expression
908 #define BOOST_FOREACH_TYPEOF(COL) \
909 (true ? 0 : boost::foreach_detail_::encode_type(COL, boost::foreach_detail_::is_const_(COL)))
911 // returns true_* if the type is noncopyable
912 #define BOOST_FOREACH_IS_NONCOPYABLE(COL) \
913 boost_foreach_is_noncopyable( \
914 boost::foreach_detail_::to_ptr(COL) \
915 , boost_foreach_argument_dependent_lookup_hack_value)
917 // returns true_* if the type is a lightweight proxy (and is not noncopyable)
918 #define BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL) \
919 boost::foreach_detail_::and_( \
920 boost::foreach_detail_::not_(BOOST_FOREACH_IS_NONCOPYABLE(COL)) \
921 , boost_foreach_is_lightweight_proxy( \
922 boost::foreach_detail_::to_ptr(COL) \
923 , boost_foreach_argument_dependent_lookup_hack_value))
925 #if defined(BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION)
926 ///////////////////////////////////////////////////////////////////////////////
927 // R-values and const R-values supported here with zero runtime overhead
928 ///////////////////////////////////////////////////////////////////////////////
930 // No variable is needed to track the rvalue-ness of the collection expression
931 # define BOOST_FOREACH_PREAMBLE() \
932 BOOST_FOREACH_SUPPRESS_WARNINGS()
934 // Evaluate the collection expression
935 # define BOOST_FOREACH_EVALUATE(COL) \
938 # define BOOST_FOREACH_SHOULD_COPY(COL) \
939 (true ? 0 : boost::foreach_detail_::or_( \
940 BOOST_FOREACH_IS_RVALUE(COL) \
941 , BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL)))
943 #elif defined(BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION)
944 ///////////////////////////////////////////////////////////////////////////////
945 // R-values and const R-values supported here
946 ///////////////////////////////////////////////////////////////////////////////
948 // Declare a variable to track the rvalue-ness of the collection expression
949 # define BOOST_FOREACH_PREAMBLE() \
950 BOOST_FOREACH_SUPPRESS_WARNINGS() \
951 if (bool BOOST_FOREACH_ID(_foreach_is_rvalue) = false) {} else
953 // Evaluate the collection expression, and detect if it is an lvalue or and rvalue
954 # define BOOST_FOREACH_EVALUATE(COL) \
955 (true ? boost::foreach_detail_::make_probe((COL), BOOST_FOREACH_ID(_foreach_is_rvalue)) : (COL))
957 // The rvalue/lvalue-ness of the collection expression is determined dynamically, unless
958 // type type is an array or is noncopyable or is non-const, in which case we know it's an lvalue.
959 // If the type happens to be a lightweight proxy, always make a copy.
960 # define BOOST_FOREACH_SHOULD_COPY(COL) \
961 (boost::foreach_detail_::should_copy_impl( \
962 true ? 0 : boost::foreach_detail_::or_( \
963 boost::foreach_detail_::is_array_(COL) \
964 , BOOST_FOREACH_IS_NONCOPYABLE(COL) \
965 , boost::foreach_detail_::not_(boost::foreach_detail_::is_const_(COL))) \
966 , true ? 0 : BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL) \
967 , &BOOST_FOREACH_ID(_foreach_is_rvalue)))
969 #elif !defined(BOOST_FOREACH_NO_RVALUE_DETECTION)
970 ///////////////////////////////////////////////////////////////////////////////
971 // R-values supported here, const R-values NOT supported here
972 ///////////////////////////////////////////////////////////////////////////////
974 // No variable is needed to track the rvalue-ness of the collection expression
975 # define BOOST_FOREACH_PREAMBLE() \
976 BOOST_FOREACH_SUPPRESS_WARNINGS()
978 // Evaluate the collection expression
979 # define BOOST_FOREACH_EVALUATE(COL) \
982 // Determine whether the collection expression is an lvalue or an rvalue.
983 // NOTE: this gets the answer wrong for const rvalues.
984 # define BOOST_FOREACH_SHOULD_COPY(COL) \
985 (true ? 0 : boost::foreach_detail_::or_( \
986 boost::foreach_detail_::is_rvalue_((COL), 0) \
987 , BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL)))
990 ///////////////////////////////////////////////////////////////////////////////
991 // R-values NOT supported here
992 ///////////////////////////////////////////////////////////////////////////////
994 // No variable is needed to track the rvalue-ness of the collection expression
995 # define BOOST_FOREACH_PREAMBLE() \
996 BOOST_FOREACH_SUPPRESS_WARNINGS()
998 // Evaluate the collection expression
999 # define BOOST_FOREACH_EVALUATE(COL) \
1002 // Can't use rvalues with BOOST_FOREACH (unless they are lightweight proxies)
1003 # define BOOST_FOREACH_SHOULD_COPY(COL) \
1004 (true ? 0 : BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL))
1008 #define BOOST_FOREACH_CONTAIN(COL) \
1009 boost::foreach_detail_::contain( \
1010 BOOST_FOREACH_EVALUATE(COL) \
1011 , BOOST_FOREACH_SHOULD_COPY(COL))
1013 #define BOOST_FOREACH_BEGIN(COL) \
1014 boost::foreach_detail_::begin( \
1015 BOOST_FOREACH_ID(_foreach_col) \
1016 , BOOST_FOREACH_TYPEOF(COL) \
1017 , BOOST_FOREACH_SHOULD_COPY(COL))
1019 #define BOOST_FOREACH_END(COL) \
1020 boost::foreach_detail_::end( \
1021 BOOST_FOREACH_ID(_foreach_col) \
1022 , BOOST_FOREACH_TYPEOF(COL) \
1023 , BOOST_FOREACH_SHOULD_COPY(COL))
1025 #define BOOST_FOREACH_DONE(COL) \
1026 boost::foreach_detail_::done( \
1027 BOOST_FOREACH_ID(_foreach_cur) \
1028 , BOOST_FOREACH_ID(_foreach_end) \
1029 , BOOST_FOREACH_TYPEOF(COL))
1031 #define BOOST_FOREACH_NEXT(COL) \
1032 boost::foreach_detail_::next( \
1033 BOOST_FOREACH_ID(_foreach_cur) \
1034 , BOOST_FOREACH_TYPEOF(COL))
1036 #define BOOST_FOREACH_DEREF(COL) \
1037 boost::foreach_detail_::deref( \
1038 BOOST_FOREACH_ID(_foreach_cur) \
1039 , BOOST_FOREACH_TYPEOF(COL))
1041 #define BOOST_FOREACH_RBEGIN(COL) \
1042 boost::foreach_detail_::rbegin( \
1043 BOOST_FOREACH_ID(_foreach_col) \
1044 , BOOST_FOREACH_TYPEOF(COL) \
1045 , BOOST_FOREACH_SHOULD_COPY(COL))
1047 #define BOOST_FOREACH_REND(COL) \
1048 boost::foreach_detail_::rend( \
1049 BOOST_FOREACH_ID(_foreach_col) \
1050 , BOOST_FOREACH_TYPEOF(COL) \
1051 , BOOST_FOREACH_SHOULD_COPY(COL))
1053 #define BOOST_FOREACH_RDONE(COL) \
1054 boost::foreach_detail_::rdone( \
1055 BOOST_FOREACH_ID(_foreach_cur) \
1056 , BOOST_FOREACH_ID(_foreach_end) \
1057 , BOOST_FOREACH_TYPEOF(COL))
1059 #define BOOST_FOREACH_RNEXT(COL) \
1060 boost::foreach_detail_::rnext( \
1061 BOOST_FOREACH_ID(_foreach_cur) \
1062 , BOOST_FOREACH_TYPEOF(COL))
1064 #define BOOST_FOREACH_RDEREF(COL) \
1065 boost::foreach_detail_::rderef( \
1066 BOOST_FOREACH_ID(_foreach_cur) \
1067 , BOOST_FOREACH_TYPEOF(COL))
1069 ///////////////////////////////////////////////////////////////////////////////
1072 // For iterating over collections. Collections can be
1073 // arrays, null-terminated strings, or STL containers.
1074 // The loop variable can be a value or reference. For
1077 // std::list<int> int_list(/*stuff*/);
1078 // BOOST_FOREACH(int &i, int_list)
1081 // * loop body goes here.
1082 // * i is a reference to the int in int_list.
1086 // Alternately, you can declare the loop variable first,
1087 // so you can access it after the loop finishes. Obviously,
1088 // if you do it this way, then the loop variable cannot be
1092 // BOOST_FOREACH(i, int_list)
1095 #define BOOST_FOREACH(VAR, COL) \
1096 BOOST_FOREACH_PREAMBLE() \
1097 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_col) = BOOST_FOREACH_CONTAIN(COL)) {} else \
1098 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_cur) = BOOST_FOREACH_BEGIN(COL)) {} else \
1099 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_end) = BOOST_FOREACH_END(COL)) {} else \
1100 for (bool BOOST_FOREACH_ID(_foreach_continue) = true; \
1101 BOOST_FOREACH_ID(_foreach_continue) && !BOOST_FOREACH_DONE(COL); \
1102 BOOST_FOREACH_ID(_foreach_continue) ? BOOST_FOREACH_NEXT(COL) : (void)0) \
1103 if (boost::foreach_detail_::set_false(BOOST_FOREACH_ID(_foreach_continue))) {} else \
1104 for (VAR = BOOST_FOREACH_DEREF(COL); !BOOST_FOREACH_ID(_foreach_continue); BOOST_FOREACH_ID(_foreach_continue) = true)
1106 ///////////////////////////////////////////////////////////////////////////////
1107 // BOOST_REVERSE_FOREACH
1109 // For iterating over collections in reverse order. In
1110 // all other respects, BOOST_REVERSE_FOREACH is like
1113 #define BOOST_REVERSE_FOREACH(VAR, COL) \
1114 BOOST_FOREACH_PREAMBLE() \
1115 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_col) = BOOST_FOREACH_CONTAIN(COL)) {} else \
1116 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_cur) = BOOST_FOREACH_RBEGIN(COL)) {} else \
1117 if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_end) = BOOST_FOREACH_REND(COL)) {} else \
1118 for (bool BOOST_FOREACH_ID(_foreach_continue) = true; \
1119 BOOST_FOREACH_ID(_foreach_continue) && !BOOST_FOREACH_RDONE(COL); \
1120 BOOST_FOREACH_ID(_foreach_continue) ? BOOST_FOREACH_RNEXT(COL) : (void)0) \
1121 if (boost::foreach_detail_::set_false(BOOST_FOREACH_ID(_foreach_continue))) {} else \
1122 for (VAR = BOOST_FOREACH_RDEREF(COL); !BOOST_FOREACH_ID(_foreach_continue); BOOST_FOREACH_ID(_foreach_continue) = true)