Boost foreach conflicts with Q_FOREACH (Qt) and moc generation?

boost, c++, point-cloud-library, qt

Solution

The problem is that Qt defines a `foreach` macro (`#define foreach Q_FOREACH`) which conflicts with the `boost::foreach` namespace.

The easiest ways to solve it is to either include Boost before Qt or simply undefine the Qt's macro before including the boost's header file. I prefer the second since it won't need extra documentation (`// remember to include Boost before Qt`), and it is easier to manage in nested header files and if you use precompiled headers.

#undef foreach
#include <boost/foreach.hpp>

This option is less invasive than disabling Qt's keywords (compilation flag `-DQT_NO_KEYWORDS`) and can be applied only in the affected files if wanted. It won't affect the use of `Q_FOREACH` (obviously if you use the Qt's `foreach` it will fail). It also works independently that Qt is included before or after `<boost/foreach.hpp>`.

Problem

I have a program edited in Vs 2008 using some libraries such as Qt and Point Cloud Library (PCL). PCL has a 3rd party library which contains boost. However, some errors appeared after compiling: 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/sequenced_index.hpp(926) : error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a type 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/sequenced_index.hpp(926) : error C2039: 'tag' : is not a member of 'boost' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/sequenced_index.hpp(926) : error C2061: syntax error : identifier 'tag' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/ordered_index.hpp(1399) : error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a type 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/ordered_index.hpp(1399) : error C2039: 'tag' : is not a member of 'boost' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/ordered_index.hpp(1399) : error C2061: syntax error : identifier 'tag' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/hashed_index.hpp(1254) : error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a type 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/hashed_index.hpp(1254) : error C2039: 'tag' : is not a member of 'boost' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/hashed_index.hpp(1254) : error C2061: syntax error : identifier 'tag' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/random_access_index.hpp(1012) : error C3083: 'Q_FOREACH': the symbol to the left of a '::' must be a type 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/random_access_index.hpp(1012) : error C2039: 'tag' : is not a member of 'boost' 1>C:\Program Files\PCL 1.5.1\3rdParty\Boost\include\boost/multi_index/random_access_index.hpp(1012) : error C2061: syntax error : identifier 'tag' For the first problem, the error location in the source file is: ``` template<typename SuperMeta,typename TagList> inline boost::mpl::true_* boost_foreach_is_noncopyable( boost::multi_index::detail::random_access_index<SuperMeta,TagList>*&, boost::foreach::tag) // <-------------error here for the first compile error. { return 0; } ``` I think maybe this indicates that the `Q_FOREACH` conflicts with the boost foreach. But I do not know how to solve this problem?

Original source

Related problems