Boost logo

Boost Users :

Subject: Re: [Boost-users] [multi-index] foreach supports breaks compilation
From: Michel Morin (mimomorin_at_[hidden])
Date: 2011-11-16 07:03:16


Szymon Gatner wrote:
> this happens because as suggested in boost.foreach docs I have this earlier:
>
> #define foreach         BOOST_FOREACH
>
> which (if I understand correctly) causes offending line to expand into:
>
> boost::BOOST_FOREACH::tag
>
> Is there anything easy I could do to fix this?

Your problem depends on the ordering of header files and the macro definition:

// OK
#define foreach BOOST_FOREACH
#include <boost/foreach.hpp>
#include <boost/multi_index/hashed_index.hpp>

// OK
#include <boost/foreach.hpp>
#include <boost/multi_index/hashed_index.hpp>
#define foreach BOOST_FOREACH

// Error
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
#include <boost/multi_index/hashed_index.hpp>

If you can control the ordering, you can avoid the problem.
But this solution is error prone.

Fortunately, is_noncopyable and is_lightweight_proxy customization points
seem to be only used in Boost.MultiIndex. So, I think, changing the name of
namespace foreach is a viable solution.
(Please note that this solution can break existing codes using
is_noncopyable and is_lightweight_proxy customization points.)

1. Change
    namespace foreach --> namespace foreach_
    foreach:: --> foreach_::
in boost/foreach.hpp and boost/foreach_fwd.hpp.

2. Change
    foreach:: --> foreach_::
in
boost/multi_index/hashed_index.hpp
boost/multi_index/ordered_index.hpp
boost/multi_index/random_access_index.hpp
boost/multi_index/sequenced_index.hpp
(and, if you want, libs/foreach/test/noncopyable.cpp).

Regards,
Michel


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net