使用BOOST_PP_SEQ_FOREACH_R递归处理枚举 [英] Using BOOST_PP_SEQ_FOREACH_R to recursively process enums

查看:393
本文介绍了使用BOOST_PP_SEQ_FOREACH_R递归处理枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与我的问题相关替换语句中扩展模板的替代方法尝试让Boost.Preprocessor在一个序列上进行嵌套的交互。

Related to my question Alternative to expanding templates in a switch statement trying to get Boost.Preprocessor to do a nested interation over a sequence.

#include <boost/preprocessor.hpp>
#include <iostream>

#define LASERTYPE_VALUES (EXCIMER)(GAS)(METALVAPOR)(SOLIDSTATE)(DYE)(SEMICONDUCTOR)(FREEELECTRON)(OTHER)

enum LaserType
{
  BOOST_PP_SEQ_ENUM(LASERTYPE_VALUES)
};

#define LT_NESTED(maR, maToplevelType, maNestedType)                    \
  std::cout << "Test nested: first=" << LaserType(maToplevelType) << " second=" << LaserType(maNestedType) << " \n";

#define LT_TOPLEVEL(maR, maUnused, maType)                              \
  std::cout << "Test toplevel: " << LaserType(maType) << " \n";         \
  BOOST_PP_SEQ_FOR_EACH_R(maR, LT_NESTED, maType, LASERTYPE_VALUES);

int main() {
  BOOST_PP_SEQ_FOR_EACH(LT_TOPLEVEL, %%, LASERTYPE_VALUES);
}

导致此错误:

test-pp.cpp: In function ‘int main()’:
test-pp.cpp:15:32: error: ‘LT_NESTED’ was not declared in this scope
   BOOST_PP_SEQ_FOR_EACH_R(maR, LT_NESTED, maType, LASERTYPE_VALUES);
                                ^
/usr/include/boost/preprocessor/seq/for_each.hpp:49:57: note: in expansion of macro ‘LT_TOPLEVEL’
 # define BOOST_PP_SEQ_FOR_EACH_M_I(r, macro, data, seq) macro(r, data, BOOST_PP_SEQ_HEAD(seq))
                                                         ^
test-pp.cpp:4:39: error: ‘EXCIMER’ cannot be used as a function
 #define LASERTYPE_VALUES (EXCIMER)(GAS)(METALVAPOR)(SOLIDSTATE)(DYE)(SEMICONDUCTOR)(FREEELECTRON)(OTHER)
                                       ^
test-pp.cpp:15:51: note: in expansion of macro ‘LASERTYPE_VALUES’
   BOOST_PP_SEQ_FOR_EACH_R(maR, LT_NESTED, maType, LASERTYPE_VALUES);

重新排序宏没有帮助。这里有一些根本的错误,它可能是微不足道的,但我还没有想出如何做到这一点,没有看到任何体面的例子。如果有人有任何建议,我会非常有兴趣知道我在这里做错什么。

Reordering the macros doesn't help. There's something fundamentally wrong here, and it's likely trivial, but I haven't yet figured out how to do this and haven't seen any decent examples. If anyone had any suggestions, I'd be very interested to know what I'm doing wrong here.

https://groups.google.com/forum/#!topic/boost-list/jhN4NE9VAtg 表示我可能遇到重新出现问题。可能我可能直接使用BOOST_PP_FOR作为顶级宏。

https://groups.google.com/forum/#!topic/boost-list/jhN4NE9VAtg indicates that I might be running into reentrancy problems. It looks like I might be able to use BOOST_PP_FOR directly for the top level macro perhaps. If anyone had an example of that, it would be really useful.

推荐答案

我在 https://groups.google.com/forum/#!topic/boost-devel-archive / Tbcs4nn4sPE 给出了这个解决方案:

I found a workaround in https://groups.google.com/forum/#!topic/boost-devel-archive/Tbcs4nn4sPE which gives this solution:

#include <boost/preprocessor.hpp>
#include <iostream>

#define LASERTYPE_VALUES (EXCIMER)(GAS)(METALVAPOR)(SOLIDSTATE)(DYE)(SEMICONDUCTOR)(FREEELECTRON)(OTHER)

enum LaserType
{
  BOOST_PP_SEQ_ENUM(LASERTYPE_VALUES)
};

#define PP_SEQ_FOR_EACH_R_ID() BOOST_PP_SEQ_FOR_EACH_R 
#define PP_DEFER(x) x BOOST_PP_EMPTY()

#define LT_NESTED(maR, maToplevelType, maNestedType)                    \
  std::cout << "Test nested: first=" << LaserType(maToplevelType) << " second=" << LaserType(maNestedType) << " \n";

#define LT_TOPLEVEL(maR, maUnused, maType)                              \
  std::cout << "Test toplevel: " << LaserType(maType) << " \n";         \
  PP_DEFER(PP_SEQ_FOR_EACH_R_ID)()(maR, LT_NESTED, maType, LASERTYPE_VALUES);

int main() {
  BOOST_PP_EXPAND(BOOST_PP_SEQ_FOR_EACH(LT_TOPLEVEL, %%, LASERTYPE_VALUES));
}

这有点深奥,但它需要编译和运行。

It's a bit esoteric but it compiles and functions as required.

这篇关于使用BOOST_PP_SEQ_FOREACH_R递归处理枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆