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

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

问题描述

有关我的问题<一href=\"http://stackoverflow.com/questions/35360908/alternative-to-expanding-templates-in-a-switch-statement/\">Alternative扩大模板在switch语句试图获得提振。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/#​​!主题/提振列表/ 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天全站免登陆