使用boost预处理器构造显式值枚举

时间:2017-12-10 09:58:02

标签: c++ boost enums macros boost-preprocessor

我正在尝试使用Boost预处理器库从enum对列表中构造(name, value)。显然,这本身并不是特别有用,但目的是将其与我编写的另一段代码链接在一起,将代码自动转换为enum值。

以下是我正在使用的简化版本。

# include <boost/preprocessor.hpp>
# include <iostream>

# define NAGA_PP_ENUM_NUMBERED_SEQ_X(s, data, elem) BOOST_PP_TUPLE_ELEM(0, elem) = BOOST_PP_TUPLE_ELEM(1, elem)

# define NAGA_PP_ENUM_NUMBERED(name, enumerators) \
    enum class name \
    { \
        BOOST_PP_SEQ_ENUM(BOOST_PP_SEQ_TRANSFORM(NAGA_PP_ENUM_NUMBERED_SEQ_X, ~, enumerators)) \
    };

NAGA_PP_ENUM_NUMBERED(Planets,
    (MERCURY, -1)
    (VENUS, 32)
    (EARTH, 39)
    (MARS, 44)
    (JUPITER, 45)
    (SATURN, 46)
    (URANUS, 48)
    (NEPTUNE, 50)
)

int main()
{
    std::cout << Planets::MARS << std::endl;    // should print out "44"
}

我期待这个扩展到这个:

enum class Planets
{
    MERCURY = -1,
    VENUS = 32,
    EARTH = 39,
    MARS = 44,
    JUPITER = 45,
    SATURN= 46,
    URANUS= 48,
    NEPTUNE = 50,
};

但是,它无效,我收到以下错误:

prog.cpp:21:1: error: macro "BOOST_PP_SEQ_SIZE_0" passed 2 arguments, but takes just 1
 )
 ^                       
prog.cpp:21:1: error: macro "BOOST_PP_SEQ_ELEM_0" passed 2 arguments, but takes just 1
 )
 ^                  
prog.cpp:21:1: error: macro "BOOST_PP_SEQ_ELEM_III" requires 2 arguments, but only 1 given
prog.cpp:21:1: error: macro "BOOST_PP_SEQ_TAIL_I" passed 2 arguments, but takes just 1
 )
 ^                       
prog.cpp:21:1: error: macro "BOOST_PP_SEQ_TAIL_I" passed 2 arguments, but takes just 1
 )
 ^             
prog.cpp:21:1: error: macro "BOOST_PP_SEQ_TAIL_I" passed 2 arguments, but takes just 1
prog.cpp:21:1: error: macro "BOOST_PP_SEQ_TAIL_I" passed 2 arguments, but takes just 1
 )
 ^                      
prog.cpp:21:1: error: macro "BOOST_PP_SEQ_TAIL_I" passed 2 arguments, but takes just 1
prog.cpp:21:1: error: macro "BOOST_PP_SEQ_TAIL_I" passed 2 arguments, but takes just 1
prog.cpp:21:1: error: macro "BOOST_PP_VARIADIC_ELEM_2" requires 4 arguments, but only 2 given
 )
 ^                       
In file included from /usr/include/boost/preprocessor/control/while.hpp:17:0,
                 from /usr/include/boost/preprocessor/arithmetic/add.hpp:20,
                 from /usr/include/boost/preprocessor/arithmetic.hpp:17,
                 from /usr/include/boost/preprocessor/library.hpp:16,
                 from /usr/include/boost/preprocessor.hpp:17,
                 from prog.cpp:1:
prog.cpp:9:27: error: expected ‘}’ before ‘BOOST_PP_SEQ_TAIL_I’
         BOOST_PP_SEQ_ENUM(BOOST_PP_SEQ_TRANSFORM(NAGA_PP_ENUM_NUMBERED_SEQ_X, ~, enumerators)) \
                           ^
prog.cpp:12:1: note: in expansion of macro ‘NAGA_PP_ENUM_NUMBERED’
 NAGA_PP_ENUM_NUMBERED(Planets,
 ^~~~~~~~~~~~~~~~~~~~~
prog.cpp:9:27: error: expected initializer before ‘BOOST_PP_VARIADIC_ELEM_2’
         BOOST_PP_SEQ_ENUM(BOOST_PP_SEQ_TRANSFORM(NAGA_PP_ENUM_NUMBERED_SEQ_X, ~, enumerators)) \
                           ^
prog.cpp:12:1: note: in expansion of macro ‘NAGA_PP_ENUM_NUMBERED’
 NAGA_PP_ENUM_NUMBERED(Planets,
 ^~~~~~~~~~~~~~~~~~~~~
prog.cpp:10:5: error: expected declaration before ‘}’ token
     };
     ^
prog.cpp:12:1: note: in expansion of macro ‘NAGA_PP_ENUM_NUMBERED’
 NAGA_PP_ENUM_NUMBERED(Planets,
 ^~~~~~~~~~~~~~~~~~~~~

我想我可能会遗漏一些有关元组序列语法应该如何工作的东西。请记住,我正在编译MSVC 2013。

感谢任何帮助或指示!

0 个答案:

没有答案