将boostforeach与本身就是模板的项目一起使用
using boost foreach with items that are themselves templates
我有一个std::deque< std::pair<int, int> >
,我想使用BOOST_FOREACH
对其进行迭代。
我尝试了以下方法:
#define foreach_ BOOST_FOREACH
// declaration of the std::deque
std::deque< std::pair<int, int> > chosen;
foreach_( std::pair<int,int> p, chosen )
{
...
}
但是当我(在Visual Studio中)编译它时,我会得到以下错误:
warning C4002: too many actual parameters for macro 'BOOST_FOREACH'
1>c:usersbeebandtests.cpp(133): error C2143: syntax error : missing ')' before '>'
1>c:usersbeebandtests.cpp(133): error C2059: syntax error : '>'
1>c:usersbeebandtests.cpp(133): error C2059: syntax error : ')'
1>c:usersbeebandtests.cpp(133): error C2143: syntax error : missing ';' before '{'
1>c:usersbeebandtests.cpp(133): error C2181: illegal else without matching if
将BOOST_FOREACH
与此deque
一起使用的正确方法是什么?
问题在于,
,因为预处理器正在使用它来分离宏参数。
使用typedef
:的可能解决方案
typedef std::pair<int, int> int_pair_t;
std::deque<int_pair_t> chosen;
foreach_( int_pair_t p, chosen )
// Or (as commented by Arne Mertz)
typedef std::deque<std::pair<int, int>> container_t;
container_t chosen;
foreach_(container_t::value_type p, chosen)
可能的替代品,都是在c++11中引入的,有:
环路范围:
for (auto& : chosen) { }
lambdas:
std::for_each(std::begin(chosen), std::end(chosen) [](std::pair<int, int>& p) { });
作为BOOST_FOREACH
的作者,我请求您停止使用它。这是一个时间来了又去的黑客。请,请,请使用C++11的范围基数进行循环,并让BOOST_FOREACH
死亡。
Boosts foreach
是宏,这意味着它由预处理器处理。预处理器非常简单,不能处理带有逗号的符号,因为它被用作宏参数分隔符。
相关文章:
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 您是否必须随项目一起交付原型文件?
- Catch2 单元测试未与 CMake 项目一起运行
- 如何将"new"公共项目对话框与 wxwidget 一起使用?
- CMake 项目结构:如何正确地将库合并在一起并将它们包含在多个可执行文件中
- 如何将 Clang 静态分析器与 Cortex-M 项目一起使用?
- 将两个数组组合在一起,形成每个项目的一对
- QT项目在配置文件上崩溃,与发布和调试器一起运行
- 如何将cacert.pem与vs2015 curl项目一起包含,因此无需指定Cainfo curl选项
- 如何连接两个文件夹/项目的生成文件,这两个文件夹/项目必须一起运行且独立运行
- 在代码块中一起编译两个项目
- "namespaced" CMake 中包含路径,并且C++项目在将项目集成在一起时是否有好处?
- 使用Java和C 一起使用Android项目
- CMake - 如何链接到我与项目一起打包的外部库
- 如何将 Google 测试与通过自动工具构建的项目一起使用
- 我如何让zxing与我的iPhone项目一起工作
- 如何在Visual Studio 2010中实现Tesseract与项目一起运行
- 将boostforeach与本身就是模板的项目一起使用
- malloc.c "sYSMALLOC: Assertion"与C++项目一起执行
- 将头文件与同一解决方案中的多个项目一起使用