简而言之,我想完成以下任务:
#define CLASS( C ) class C;
#define CLASSES( ... )
CLASSES( Foo, Bar, Golf ) // Expand: class Foo; class Bar; class Golf;
有什么方法可以使用 boost 来实现这一点吗?
目前,我已经尝试过以下方法:
#include <boost/preprocessor/repetition/repeat.hpp>
#define CLASS( C ) class C;
#define CLASSES( N, ... ) BOOST_PP_REPEAT( N, CLASS, Name) // problem here
CLASSES( 3, Foo, Bar, Golf ) // Expand: class Name; class Name; class Name;
但我不知道如何合并
__VA_ARGS__
以扩展不同的名称。
我愿意接受其他建议,不严格针对 boost,但仅限于 C++11。
这是一个 BOOST PP 示例,使用
CLASSES(x, y, z)
扩展为 class
前向声明语句。
#include <boost/preprocessor/library.hpp> // The whole BOOST_PP library (overkill).
#define CLASSES_SEMI() ;
#define CLASSES_OP(r, data, i, elem) \
BOOST_PP_IF(i, CLASSES_SEMI, BOOST_PP_EMPTY)() \
class elem \
/**/
#define CLASSES_SEQ(seq) \
BOOST_PP_SEQ_FOR_EACH_I(CLASSES_OP, /*no data*/, seq) \
/**/
#define CLASSES(...) CLASSES_SEQ(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
CLASSES(Foo, Bar, Golf);
生成:
class Foo ; class Bar ; class Golf ;