Boost.Preprocessor:BOOST_PP_TUPLE_ELEM和BOOST_PP_SEQ_ELEM

时间:2011-08-28 20:32:33

标签: c++ boost macros boost-preprocessor

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

#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

1 个答案:

答案 0 :(得分:0)

试试这个:

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

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