Using BOOST_PP_SEQ_FOREACH_R to recursively process enums

匆匆过客 提交于 2019-12-12 02:57:39

问题


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);
}

This leads to this error:

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 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.


回答1:


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.



来源:https://stackoverflow.com/questions/35753893/using-boost-pp-seq-foreach-r-to-recursively-process-enums

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!