我的问题最好用代码来描述:
#include <boost/preprocessor.hpp>
#include <iostream>
#define SEQ (1, 2)(3, 4)
int main() {
// this does not compile:
// std::cout << BOOST_PP_TUPLE_ELEM(2, 0, BOOST_PP_SEQ_ELEM(0, SEQ)) << std::endl;
// this compiles with warning C4002: too many actual parameters for macro 'BOOST_PP_SEQ_ELEM_0'
std::cout << BOOST_PP_SEQ_ELEM(0, SEQ) << std::endl;
// Output: 1
// Expected output: None, since it shouldn't compile cout << (1, 2) << std::endl
}
我究竟做错了什么?
嗯,谢谢
#define SEQ ((1, 2))((3, 4))
// ...
std::cout << BOOST_PP_TUPLE_ELEM(2, 0, BOOST_PP_SEQ_ELEM(0, SEQ)) << std::endl;
std::cout << BOOST_PP_TUPLE_ELEM(2, 1, BOOST_PP_SEQ_ELEM(0, SEQ)) << std::endl;
std::cout << BOOST_PP_TUPLE_ELEM(2, 0, BOOST_PP_SEQ_ELEM(1, SEQ)) << std::endl;
std::cout << BOOST_PP_TUPLE_ELEM(2, 1, BOOST_PP_SEQ_ELEM(1, SEQ)) << std::endl;
输出:
1
2
3
4
哪个是对的。
但这不能解决我的问题,因为我想将序列与
BOOST_FUSION_DEFINE_STRUCT
一起使用,这意味着我不能使用多余的括号。我想做这样的事情:
#define DEFINE_MY_FANCY_STRUCT(NAMESPACE_SEQ, NAME, ATTRIBUTES) \
BOOST_FUSION_DEFINE_STRUCT(NAMESPACE_SEQ, NAME, ATTRIBUTES) \
\
// other boilerplate code here, i.e. serialization with BOOST_SERIALIZATION_NVP or generation of spirit parsers
最佳答案
尝试这个:
#define SEQ ((1, 2))((3, 4))
输出应为
2
,因为(1, 2)
的结果为2
。关于c++ - Boost.Preprocessor:BOOST_PP_TUPLE_ELEM和BOOST_PP_SEQ_ELEM,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7223721/