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)
10 // Anson Tsao - for the initial inspiration and several good suggestions.
11 // Thorsten Ottosen - for Boost.Range, and for suggesting a way to detect
12 // const-qualified rvalues at compile time on VC7.1+
13 // Russell Hind - For help porting to Borland
14 // Alisdair Meredith - For help porting to Borland
15 // Stefan Slapeta - For help porting to Intel
19 // MS compatible compilers support #pragma once
20 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
25 #include <utility> // for std::pair
27 #include <boost/config.hpp>
28 #include <boost/detail/workaround.hpp>
31 // Some compilers let us detect even const-qualified rvalues at compile-time
32 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1310) \
33 || BOOST_WORKAROUND(__GNUC__, >= 4) \
34 || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ >= 4))
35 # define BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION
37 // Some compilers allow temporaries to be bound to non-const references.
38 // These compilers make it impossible to for BOOST_FOREACH to detect
39 // temporaries and avoid reevaluation of the collection expression.
40 # if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) \
41 || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) \
42 || (BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, <= 700) && defined(_MSC_VER)) \
43 || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x570)) \
44 || BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
45 # define BOOST_FOREACH_NO_RVALUE_DETECTION
47 // Some compilers do not correctly implement the lvalue/rvalue conversion
48 // rules of the ternary conditional operator.
49 # if defined(BOOST_FOREACH_NO_RVALUE_DETECTION) \
50 || defined(BOOST_NO_SFINAE) \
51 || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1400)) \
52 || BOOST_WORKAROUND(BOOST_INTEL_WIN, <= 810) \
53 || BOOST_WORKAROUND(__GNUC__, < 3) \
54 || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ <= 2)) \
55 || (BOOST_WORKAROUND(__GNUC__, == 3) && (__GNUC_MINOR__ <= 3) && defined(__APPLE_CC__)) \
56 || BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600)) \
57 || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
58 # define BOOST_FOREACH_NO_CONST_RVALUE_DETECTION
60 # define BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
65 #include <boost/mpl/if.hpp>
66 #include <boost/mpl/logical.hpp>
67 #include <boost/mpl/eval_if.hpp>
68 #include <boost/noncopyable.hpp>
69 #include <boost/range/end.hpp>
70 #include <boost/range/begin.hpp>
71 #include <boost/range/result_iterator.hpp>
72 #include <boost/type_traits/is_array.hpp>
73 #include <boost/type_traits/is_const.hpp>
74 #include <boost/type_traits/is_abstract.hpp>
75 #include <boost/type_traits/is_base_and_derived.hpp>
76 #include <boost/iterator/iterator_traits.hpp>
77 #include <boost/utility/addressof.hpp>
79 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
81 # include <boost/aligned_storage.hpp>
82 # include <boost/utility/enable_if.hpp>
83 # include <boost/type_traits/remove_const.hpp>
86 // This must be at global scope, hence the uglified name
87 enum boost_foreach_argument_dependent_lookup_hack
89 boost_foreach_argument_dependent_lookup_hack_value
95 // forward declarations for iterator_range
99 // forward declarations for sub_range
105 ///////////////////////////////////////////////////////////////////////////////
109 inline std::pair
<T
, T
> in_range(T begin
, T end
)
111 return std::make_pair(begin
, end
);
114 ///////////////////////////////////////////////////////////////////////////////
115 // boost::foreach::tag
117 typedef boost_foreach_argument_dependent_lookup_hack tag
;
119 ///////////////////////////////////////////////////////////////////////////////
120 // boost::foreach::is_lightweight_proxy
121 // Specialize this for user-defined collection types if they are inexpensive to copy.
122 // This tells BOOST_FOREACH it can avoid the rvalue/lvalue detection stuff.
124 struct is_lightweight_proxy
129 ///////////////////////////////////////////////////////////////////////////////
130 // boost::foreach::is_noncopyable
131 // Specialize this for user-defined collection types if they cannot be copied.
132 // This also tells BOOST_FOREACH to avoid the rvalue/lvalue detection stuff.
134 struct is_noncopyable
135 #if !defined(BOOST_BROKEN_IS_BASE_AND_DERIVED) && !defined(BOOST_NO_IS_ABSTRACT)
137 boost::is_abstract
<T
>
138 , boost::is_base_and_derived
<boost::noncopyable
, T
>
140 #elif !defined(BOOST_BROKEN_IS_BASE_AND_DERIVED)
141 : boost::is_base_and_derived
<boost::noncopyable
, T
>
142 #elif !defined(BOOST_NO_IS_ABSTRACT)
143 : boost::is_abstract
<T
>
150 } // namespace foreach
154 // vc6/7 needs help ordering the following overloads
155 #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
156 # define BOOST_FOREACH_TAG_DEFAULT ...
158 # define BOOST_FOREACH_TAG_DEFAULT boost::foreach::tag
161 ///////////////////////////////////////////////////////////////////////////////
162 // boost_foreach_is_lightweight_proxy
163 // Another customization point for the is_lightweight_proxy optimization,
164 // this one works on legacy compilers. Overload boost_foreach_is_lightweight_proxy
165 // at the global namespace for your type.
167 inline boost::foreach::is_lightweight_proxy
<T
> *
168 boost_foreach_is_lightweight_proxy(T
*&, BOOST_FOREACH_TAG_DEFAULT
) { return 0; }
171 inline boost::mpl::true_
*
172 boost_foreach_is_lightweight_proxy(std::pair
<T
, T
> *&, boost::foreach::tag
) { return 0; }
175 inline boost::mpl::true_
*
176 boost_foreach_is_lightweight_proxy(boost::iterator_range
<T
> *&, boost::foreach::tag
) { return 0; }
179 inline boost::mpl::true_
*
180 boost_foreach_is_lightweight_proxy(boost::sub_range
<T
> *&, boost::foreach::tag
) { return 0; }
183 inline boost::mpl::true_
*
184 boost_foreach_is_lightweight_proxy(T
**&, boost::foreach::tag
) { return 0; }
186 ///////////////////////////////////////////////////////////////////////////////
187 // boost_foreach_is_noncopyable
188 // Another customization point for the is_noncopyable trait,
189 // this one works on legacy compilers. Overload boost_foreach_is_noncopyable
190 // at the global namespace for your type.
192 inline boost::foreach::is_noncopyable
<T
> *
193 boost_foreach_is_noncopyable(T
*&, BOOST_FOREACH_TAG_DEFAULT
) { return 0; }
198 namespace foreach_detail_
201 ///////////////////////////////////////////////////////////////////////////////
202 // Define some utilities for assessing the properties of expressions
204 typedef char yes_type
;
205 typedef char (&no_type
)[2];
206 yes_type
is_true(boost::mpl::true_
*);
207 no_type
is_true(boost::mpl::false_
*);
209 // Extracts the desired property from the expression without evaluating it
210 #define BOOST_FOREACH_PROTECT(expr) \
211 (static_cast<boost::mpl::bool_<1 == sizeof(boost::foreach_detail_::is_true(expr))> *>(0))
213 template<typename Bool1
, typename Bool2
>
214 inline boost::mpl::and_
<Bool1
, Bool2
> *and_(Bool1
*, Bool2
*) { return 0; }
216 template<typename Bool1
, typename Bool2
, typename Bool3
>
217 inline boost::mpl::and_
<Bool1
, Bool2
, Bool3
> *and_(Bool1
*, Bool2
*, Bool3
*) { return 0; }
219 template<typename Bool1
, typename Bool2
>
220 inline boost::mpl::or_
<Bool1
, Bool2
> *or_(Bool1
*, Bool2
*) { return 0; }
222 template<typename Bool1
, typename Bool2
, typename Bool3
>
223 inline boost::mpl::or_
<Bool1
, Bool2
, Bool3
> *or_(Bool1
*, Bool2
*, Bool3
*) { return 0; }
225 template<typename Bool
>
226 inline boost::mpl::not_
<Bool
> *not_(Bool
*) { return 0; }
229 inline boost::mpl::false_
*is_rvalue_(T
&, int) { return 0; }
232 inline boost::mpl::true_
*is_rvalue_(T
const &, ...) { return 0; }
235 inline boost::is_array
<T
> *is_array_(T
const &) { return 0; }
238 inline boost::is_const
<T
> *is_const_(T
&) { return 0; }
240 #ifndef BOOST_FOREACH_NO_RVALUE_DETECTION
242 inline boost::mpl::true_
*is_const_(T
const &) { return 0; }
245 ///////////////////////////////////////////////////////////////////////////////
246 // auto_any_t/auto_any
247 // General utility for putting an object of any type into automatic storage
250 // auto_any_base must evaluate to false in boolean context so that
251 // they can be declared in if() statements.
252 operator bool() const
259 struct auto_any
: auto_any_base
266 // temporaries of type auto_any will be bound to const auto_any_base
267 // references, but we still want to be able to mutate the stored
268 // data, so declare it as mutable.
272 typedef auto_any_base
const &auto_any_t
;
274 template<typename T
, typename C
>
275 inline BOOST_DEDUCED_TYPENAME
boost::mpl::if_
<C
, T
const, T
>::type
&auto_any_cast(auto_any_t a
)
277 return static_cast<auto_any
<T
> const &>(a
).item
;
280 typedef boost::mpl::true_ const_
;
282 ///////////////////////////////////////////////////////////////////////////////
285 template<typename T
, typename C
= boost::mpl::false_
>
287 : boost::mpl::if_
<C
, T
const, T
>
291 template<typename T
, typename C
= boost::mpl::false_
>
292 struct foreach_iterator
294 typedef BOOST_DEDUCED_TYPENAME
boost::mpl::eval_if
<
296 , range_const_iterator
<T
>
301 template<typename T
, typename C
= boost::mpl::false_
>
302 struct foreach_reference
303 : iterator_reference
<BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, C
>::type
>
307 ///////////////////////////////////////////////////////////////////////////////
311 inline type2type
<T
> *encode_type(T
&, boost::mpl::false_
*) { return 0; }
314 inline type2type
<T
, const_
> *encode_type(T
const &, boost::mpl::true_
*) { return 0; }
316 ///////////////////////////////////////////////////////////////////////////////
319 inline bool set_false(bool &b
) { return b
= false; }
321 ///////////////////////////////////////////////////////////////////////////////
325 inline T
*&to_ptr(T
const &)
331 // Borland needs a little extra help with arrays
332 #if 0 // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
333 template<typename T
,std::size_t N
>
334 inline T (*&to_ptr(T (&)[N
]))[N
]
336 static T (*t
)[N
] = 0;
341 ///////////////////////////////////////////////////////////////////////////////
345 inline T
&derefof(T
*t
)
347 // This is a work-around for a compiler bug in Borland. If T* is a pointer to array type U(*)[N],
348 // then dereferencing it results in a U* instead of U(&)[N]. The cast forces the issue.
349 return reinterpret_cast<T
&>(
351 reinterpret_cast<char const volatile *>(t
)
356 #ifdef BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION
357 ///////////////////////////////////////////////////////////////////////////////
358 // Detect at compile-time whether an expression yields an rvalue or
359 // an lvalue. This is rather non-standard, but some popular compilers
361 ///////////////////////////////////////////////////////////////////////////////
363 ///////////////////////////////////////////////////////////////////////////////
369 struct private_type_
{};
370 // can't ever return an array by value
371 typedef BOOST_DEDUCED_TYPENAME
boost::mpl::if_
<
372 boost::mpl::or_
<boost::is_abstract
<T
>, boost::is_array
<T
> >, private_type_
, T
374 operator value_type();
375 operator T
&() const;
379 rvalue_probe
<T
> const make_probe(T
const &t
);
381 # define BOOST_FOREACH_IS_RVALUE(COL) \
382 boost::foreach_detail_::and_( \
383 boost::foreach_detail_::not_(boost::foreach_detail_::is_array_(COL)) \
384 , BOOST_FOREACH_PROTECT(boost::foreach_detail_::is_rvalue_( \
385 (true ? boost::foreach_detail_::make_probe(COL) : (COL)), 0)))
387 #elif defined(BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION)
388 ///////////////////////////////////////////////////////////////////////////////
389 // Detect at run-time whether an expression yields an rvalue
390 // or an lvalue. This is 100% standard C++, but not all compilers
391 // accept it. Also, it causes FOREACH to break when used with non-
392 // copyable collection types.
393 ///////////////////////////////////////////////////////////////////////////////
395 ///////////////////////////////////////////////////////////////////////////////
401 rvalue_probe(T
&t
, bool &b
)
407 struct private_type_
{};
408 // can't ever return an array or an abstract type by value
409 #ifdef BOOST_NO_IS_ABSTRACT
410 typedef BOOST_DEDUCED_TYPENAME
boost::mpl::if_
<
411 boost::is_array
<T
>, private_type_
, T
414 typedef BOOST_DEDUCED_TYPENAME
boost::mpl::if_
<
415 boost::mpl::or_
<boost::is_abstract
<T
>, boost::is_array
<T
> >, private_type_
, T
419 operator value_type()
421 this->is_rvalue
= true;
436 rvalue_probe
<T
> make_probe(T
&t
, bool &b
) { return rvalue_probe
<T
>(t
, b
); }
439 rvalue_probe
<T
const> make_probe(T
const &t
, bool &b
) { return rvalue_probe
<T
const>(t
, b
); }
441 ///////////////////////////////////////////////////////////////////////////////
443 // holds either a T or a T const*
445 struct simple_variant
447 simple_variant(T
const *t
)
450 *static_cast<T
const **>(this->data
.address()) = t
;
453 simple_variant(T
const &t
)
456 ::new(this->data
.address()) T(t
);
459 simple_variant(simple_variant
const &that
)
460 : is_rvalue(that
.is_rvalue
)
463 ::new(this->data
.address()) T(*that
.get());
465 *static_cast<T
const **>(this->data
.address()) = that
.get();
477 return static_cast<T
const *>(this->data
.address());
479 return *static_cast<T
const * const *>(this->data
.address());
483 enum size_type
{ size
= sizeof(T
) > sizeof(T
*) ? sizeof(T
) : sizeof(T
*) };
484 simple_variant
&operator =(simple_variant
const &);
485 bool const is_rvalue
;
486 aligned_storage
<size
> data
;
489 // If the collection is an array or is noncopyable, it must be an lvalue.
490 // If the collection is a lightweight proxy, treat it as an rvalue
491 // BUGBUG what about a noncopyable proxy?
492 template<typename LValue
, typename IsProxy
>
493 inline BOOST_DEDUCED_TYPENAME
boost::enable_if
<boost::mpl::or_
<LValue
, IsProxy
>, IsProxy
>::type
*
494 should_copy_impl(LValue
*, IsProxy
*, bool *)
499 // Otherwise, we must determine at runtime whether it's an lvalue or rvalue
501 should_copy_impl(boost::mpl::false_
*, boost::mpl::false_
*, bool *is_rvalue
)
508 ///////////////////////////////////////////////////////////////////////////////
512 inline auto_any
<T
> contain(T
const &t
, boost::mpl::true_
*) // rvalue
518 inline auto_any
<T
*> contain(T
&t
, boost::mpl::false_
*) // lvalue
520 // Cannot seem to get sunpro to handle addressof() with array types.
521 #if 0 // BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x570))
524 return boost::addressof(t
);
528 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
530 auto_any
<simple_variant
<T
> >
531 contain(T
const &t
, bool *rvalue
)
533 return *rvalue
? simple_variant
<T
>(t
) : simple_variant
<T
>(&t
);
537 /////////////////////////////////////////////////////////////////////////////
540 template<typename T
, typename C
>
541 inline auto_any
<BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, C
>::type
>
542 begin(auto_any_t col
, type2type
<T
, C
> *, boost::mpl::true_
*) // rvalue
544 return boost::begin(auto_any_cast
<T
, C
>(col
));
547 template<typename T
, typename C
>
548 inline auto_any
<BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, C
>::type
>
549 begin(auto_any_t col
, type2type
<T
, C
> *, boost::mpl::false_
*) // lvalue
551 typedef BOOST_DEDUCED_TYPENAME type2type
<T
, C
>::type type
;
552 typedef BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, C
>::type iterator
;
553 return iterator(boost::begin(derefof(auto_any_cast
<type
*, boost::mpl::false_
>(col
))));
556 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
558 auto_any
<BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, const_
>::type
>
559 begin(auto_any_t col
, type2type
<T
, const_
> *, bool *)
561 return boost::begin(*auto_any_cast
<simple_variant
<T
>, boost::mpl::false_
>(col
).get());
565 ///////////////////////////////////////////////////////////////////////////////
568 template<typename T
, typename C
>
569 inline auto_any
<BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, C
>::type
>
570 end(auto_any_t col
, type2type
<T
, C
> *, boost::mpl::true_
*) // rvalue
572 return boost::end(auto_any_cast
<T
, C
>(col
));
575 template<typename T
, typename C
>
576 inline auto_any
<BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, C
>::type
>
577 end(auto_any_t col
, type2type
<T
, C
> *, boost::mpl::false_
*) // lvalue
579 typedef BOOST_DEDUCED_TYPENAME type2type
<T
, C
>::type type
;
580 typedef BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, C
>::type iterator
;
581 return iterator(boost::end(derefof(auto_any_cast
<type
*, boost::mpl::false_
>(col
))));
584 #ifdef BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION
586 auto_any
<BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, const_
>::type
>
587 end(auto_any_t col
, type2type
<T
, const_
> *, bool *)
589 return boost::end(*auto_any_cast
<simple_variant
<T
>, boost::mpl::false_
>(col
).get());
593 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
594 template<typename T
, typename C
>
596 end(auto_any_t col
, type2type
<T
*, C
> *, boost::mpl::true_
*) // null-terminated C-style strings
598 return 0; // not used
602 ///////////////////////////////////////////////////////////////////////////////
605 template<typename T
, typename C
>
606 inline bool done(auto_any_t cur
, auto_any_t end
, type2type
<T
, C
> *)
608 typedef BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, C
>::type iter_t
;
609 return auto_any_cast
<iter_t
, boost::mpl::false_
>(cur
) == auto_any_cast
<iter_t
, boost::mpl::false_
>(end
);
612 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
613 template<typename T
, typename C
>
614 inline bool done(auto_any_t cur
, auto_any_t
, type2type
<T
*, C
> *) // null-terminated C-style strings
616 return ! *auto_any_cast
<T
*, boost::mpl::false_
>(cur
);
620 ///////////////////////////////////////////////////////////////////////////////
623 template<typename T
, typename C
>
624 inline void next(auto_any_t cur
, type2type
<T
, C
> *)
626 typedef BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, C
>::type iter_t
;
627 ++auto_any_cast
<iter_t
, boost::mpl::false_
>(cur
);
630 ///////////////////////////////////////////////////////////////////////////////
633 template<typename T
, typename C
>
634 inline BOOST_DEDUCED_TYPENAME foreach_reference
<T
, C
>::type
635 deref(auto_any_t cur
, type2type
<T
, C
> *)
637 typedef BOOST_DEDUCED_TYPENAME foreach_iterator
<T
, C
>::type iter_t
;
638 return *auto_any_cast
<iter_t
, boost::mpl::false_
>(cur
);
641 } // namespace foreach_detail_
644 // A sneaky way to get the type of the collection without evaluating the expression
645 #define BOOST_FOREACH_TYPEOF(COL) \
646 (true ? 0 : boost::foreach_detail_::encode_type(COL, boost::foreach_detail_::is_const_(COL)))
648 // returns true_* if the type is noncopyable
649 #define BOOST_FOREACH_IS_NONCOPYABLE(COL) \
650 boost_foreach_is_noncopyable( \
651 boost::foreach_detail_::to_ptr(COL) \
652 , boost_foreach_argument_dependent_lookup_hack_value)
654 // returns true_* if the type is a lightweight proxy (and is not noncopyable)
655 #define BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL) \
656 boost::foreach_detail_::and_( \
657 boost::foreach_detail_::not_(BOOST_FOREACH_IS_NONCOPYABLE(COL)) \
658 , boost_foreach_is_lightweight_proxy( \
659 boost::foreach_detail_::to_ptr(COL) \
660 , boost_foreach_argument_dependent_lookup_hack_value))
662 #ifdef BOOST_FOREACH_COMPILE_TIME_CONST_RVALUE_DETECTION
663 ///////////////////////////////////////////////////////////////////////////////
664 // R-values and const R-values supported here with zero runtime overhead
665 ///////////////////////////////////////////////////////////////////////////////
667 // No variable is needed to track the rvalue-ness of the collection expression
668 # define BOOST_FOREACH_PREAMBLE() \
671 // Evaluate the collection expression
672 # define BOOST_FOREACH_EVALUATE(COL) \
675 # define BOOST_FOREACH_SHOULD_COPY(COL) \
676 (true ? 0 : boost::foreach_detail_::or_( \
677 BOOST_FOREACH_IS_RVALUE(COL) \
678 , BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL)))
680 #elif defined(BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION)
681 ///////////////////////////////////////////////////////////////////////////////
682 // R-values and const R-values supported here
683 ///////////////////////////////////////////////////////////////////////////////
685 // Declare a variable to track the rvalue-ness of the collection expression
686 # define BOOST_FOREACH_PREAMBLE() \
687 if (bool _foreach_is_rvalue = false) {} else
689 // Evaluate the collection expression, and detect if it is an lvalue or and rvalue
690 # define BOOST_FOREACH_EVALUATE(COL) \
691 (true ? boost::foreach_detail_::make_probe((COL), _foreach_is_rvalue) : (COL))
693 // The rvalue/lvalue-ness of the collection expression is determined dynamically, unless
694 // type type is an array or is noncopyable or is non-const, in which case we know it's an lvalue.
695 // If the type happens to be a lightweight proxy, always make a copy.
696 # define BOOST_FOREACH_SHOULD_COPY(COL) \
697 (boost::foreach_detail_::should_copy_impl( \
698 true ? 0 : boost::foreach_detail_::or_( \
699 boost::foreach_detail_::is_array_(COL) \
700 , BOOST_FOREACH_IS_NONCOPYABLE(COL) \
701 , boost::foreach_detail_::not_(boost::foreach_detail_::is_const_(COL))) \
702 , true ? 0 : BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL) \
703 , &_foreach_is_rvalue))
705 #elif !defined(BOOST_FOREACH_NO_RVALUE_DETECTION)
706 ///////////////////////////////////////////////////////////////////////////////
707 // R-values supported here, const R-values NOT supported here
708 ///////////////////////////////////////////////////////////////////////////////
710 // No variable is needed to track the rvalue-ness of the collection expression
711 # define BOOST_FOREACH_PREAMBLE() \
714 // Evaluate the collection expression
715 # define BOOST_FOREACH_EVALUATE(COL) \
718 // Determine whether the collection expression is an lvalue or an rvalue.
719 // NOTE: this gets the answer wrong for const rvalues.
720 # define BOOST_FOREACH_SHOULD_COPY(COL) \
721 (true ? 0 : boost::foreach_detail_::or_( \
722 boost::foreach_detail_::is_rvalue_((COL), 0) \
723 , BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL)))
726 ///////////////////////////////////////////////////////////////////////////////
727 // R-values NOT supported here
728 ///////////////////////////////////////////////////////////////////////////////
730 // No variable is needed to track the rvalue-ness of the collection expression
731 # define BOOST_FOREACH_PREAMBLE() \
734 // Evaluate the collection expression
735 # define BOOST_FOREACH_EVALUATE(COL) \
738 // Can't use rvalues with BOOST_FOREACH (unless they are lightweight proxies)
739 # define BOOST_FOREACH_SHOULD_COPY(COL) \
740 (true ? 0 : BOOST_FOREACH_IS_LIGHTWEIGHT_PROXY(COL))
744 #define BOOST_FOREACH_CONTAIN(COL) \
745 boost::foreach_detail_::contain( \
746 BOOST_FOREACH_EVALUATE(COL) \
747 , BOOST_FOREACH_SHOULD_COPY(COL))
749 #define BOOST_FOREACH_BEGIN(COL) \
750 boost::foreach_detail_::begin( \
752 , BOOST_FOREACH_TYPEOF(COL) \
753 , BOOST_FOREACH_SHOULD_COPY(COL))
755 #define BOOST_FOREACH_END(COL) \
756 boost::foreach_detail_::end( \
758 , BOOST_FOREACH_TYPEOF(COL) \
759 , BOOST_FOREACH_SHOULD_COPY(COL))
761 #define BOOST_FOREACH_DONE(COL) \
762 boost::foreach_detail_::done( \
765 , BOOST_FOREACH_TYPEOF(COL))
767 #define BOOST_FOREACH_NEXT(COL) \
768 boost::foreach_detail_::next( \
770 , BOOST_FOREACH_TYPEOF(COL))
772 #define BOOST_FOREACH_DEREF(COL) \
773 boost::foreach_detail_::deref( \
775 , BOOST_FOREACH_TYPEOF(COL))
777 ///////////////////////////////////////////////////////////////////////////////
780 // For iterating over collections. Collections can be
781 // arrays, null-terminated strings, or STL containers.
782 // The loop variable can be a value or reference. For
785 // std::list<int> int_list(/*stuff*/);
786 // BOOST_FOREACH(int &i, int_list)
789 // * loop body goes here.
790 // * i is a reference to the int in int_list.
794 // Alternately, you can declare the loop variable first,
795 // so you can access it after the loop finishes. Obviously,
796 // if you do it this way, then the loop variable cannot be
800 // BOOST_FOREACH(i, int_list)
803 #define BOOST_FOREACH(VAR, COL) \
804 BOOST_FOREACH_PREAMBLE() \
805 if (boost::foreach_detail_::auto_any_t _foreach_col = BOOST_FOREACH_CONTAIN(COL)) {} else \
806 if (boost::foreach_detail_::auto_any_t _foreach_cur = BOOST_FOREACH_BEGIN(COL)) {} else \
807 if (boost::foreach_detail_::auto_any_t _foreach_end = BOOST_FOREACH_END(COL)) {} else \
808 for (bool _foreach_continue = true; \
809 _foreach_continue && !BOOST_FOREACH_DONE(COL); \
810 _foreach_continue ? BOOST_FOREACH_NEXT(COL) : (void)0) \
811 if (boost::foreach_detail_::set_false(_foreach_continue)) {} else \
812 for (VAR = BOOST_FOREACH_DEREF(COL); !_foreach_continue; _foreach_continue = true)