1

我的问题最好用代码来描述:

#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
4

1 回答 1

0

尝试这个:

#define SEQ ((1, 2))((3, 4))

输出应该是2因为(1, 2)is的结果2

于 2011-08-28T20:58:45.527 回答