C++ 预处理器:Boost_PP_TUPLE_ELEM和Boost_PP_SEQ_ELEM

C++ 预处理器:Boost_PP_TUPLE_ELEM和Boost_PP_SEQ_ELEM,c++,boost,macros,boost-preprocessor,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;

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

#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
}
这是正确的。 但这并不能解决我的问题,因为我想使用带有
BOOST\u FUSION\u DEFINE\u 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

我知道这是一个有点晚的回复,但您可以编写一个将添加额外括号的帮助器宏。如果没有这个宏,您的特定示例可以写成BOOST_PP_TUPLE_ELEM(2,0,(BOOST_PP_SEQ_ELEM(0,SEQ))//注意添加的括号!
#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))