使用可变参数宏或模板来实现一组函数

use*_*028 5 c++ variadic-templates variadic-macros

我有一组用于实例化和初始化一组对象的方法.它们看起来几乎相同,除了传递给Init函数的参数数量:

ObjectType* CreateObjectType(Arg1 a1, Arg2 arg2, ... ArgN aN)
{
    ObjectType* object = new ObjectType();
    [...]
    object->Init(this, a1, a2, ..., aN);
    [...]
    return object;
}
Run Code Online (Sandbox Code Playgroud)

请注意,除了要传递给Init函数之外,不能在任何地方使用参数.

我想找到一种方法来实现所有这些,而无需为每个对象类型复制代码.


我尝试使用可变参数宏,结果如下(无效):

#define CREATE_OBJECT_IMPL(ObjectType, ...)    \
ObjectType* Create##ObjectType##(__VA_ARGS__)  \
{                                              \
    ObjectType* object = new ObjectType();     \
    [...]
    object->Init(this, ##__VA_ARGS__);         \
    [...]
    return object;                             \
}

// This is the result I am trying to achieve :
CREATE_OBJECT_IMPL(MyFirstObject, bool, float)
CREATE_OBJECT_IMPL(MySecondObject, int)
CREATE_OBJECT_IMPL(MyThirdObject)
Run Code Online (Sandbox Code Playgroud)

现在,在这个实现中,我使用VA_ARGS两次,两次都不正确:

  • 在第一种情况下,我想要一个包含我指定类型的参数列表(Arg1 a1,Arg2 a2 ......)

  • 在第二种情况下,我想通过它们的名称(Init(a1,a2 ...))来调用这些参数.


我尝试使用可变参数模板:

template< typename ObjectType, typename... Args >
ObjectType* CreateObject(Args args)
{
    ObjectType* object = new ObjectType();
    [...]
    object->Init(this, args);
    [...]
    return object;
}

#define CREATE_OBJECT_IMPL(ObjectType, ...)                     \
ObjectType* Create##ObjectType##(__VA_ARGS__)                   \
{                                                               \
    return CreateObject<ObjectType, __VA_ARGS__>(__VA_ARGS__);  \
}
Run Code Online (Sandbox Code Playgroud)

...但这似乎不起作用,我在模板定义行上得到以下错误:

错误C2143:语法错误:在'...'之前缺少','

错误C2065:'Args':未声明的标识符

我正在使用VS2012.

我仍然可以为每个参数编写N个类似的宏,但是我想知道是否有办法在不重复代码的情况下获得相同的结果?

Pau*_* II 6

有几种方法可以解决这个问题.首先,您可以在宏中使用类型化表达式,以便您可以解析该类型.所以CREATE_OBJECT_IMPL会这样调用:

CREATE_OBJECT_IMPL(Object, (Arg1) arg1, (Arg2) arg2)
Run Code Online (Sandbox Code Playgroud)

以下是一些将检索类型并剥离类型的宏:

#define EAT(x)
#define REM(x) x
#define STRIP(x) EAT x
#define PAIR(x) REM x
Run Code Online (Sandbox Code Playgroud)

这些宏的工作原理如下.当你写STRIP((Arg1) arg1)它会扩展到arg1.当你写PAIR((Arg1) arg1)它时会扩展到Arg1 arg1.接下来,您将要做的是将这些宏应用于传入的每个参数,因此这里有一个简单的APPLY宏,可以让您为最多8个参数执行此操作:

/* This counts the number of args */
#define NARGS_SEQ(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
#define NARGS(...) NARGS_SEQ(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1)

/* This will let macros expand before concating them */
#define PRIMITIVE_CAT(x, y) x ## y
#define CAT(x, y) PRIMITIVE_CAT(x, y)

/* This will call a macro on each argument passed in */
#define APPLY(macro, ...) CAT(APPLY_, NARGS(__VA_ARGS__))(macro, __VA_ARGS__)
#define APPLY_1(m, x1) m(x1)
#define APPLY_2(m, x1, x2) m(x1), m(x2)
#define APPLY_3(m, x1, x2, x3) m(x1), m(x2), m(x3)
#define APPLY_4(m, x1, x2, x3, x4) m(x1), m(x2), m(x3), m(x4)
#define APPLY_5(m, x1, x2, x3, x4, x5) m(x1), m(x2), m(x3), m(x4), m(x5)
#define APPLY_6(m, x1, x2, x3, x4, x5, x6) m(x1), m(x2), m(x3), m(x4), m(x5), m(x6)
#define APPLY_7(m, x1, x2, x3, x4, x5, x6, x7) m(x1), m(x2), m(x3), m(x4), m(x5), m(x6), m(x7)
#define APPLY_8(m, x1, x2, x3, x4, x5, x6, x7, x8) m(x1), m(x2), m(x3), m(x4), m(x5), m(x6), m(x7), m(x8)
Run Code Online (Sandbox Code Playgroud)

然后你可以这样定义CREATE_OBJECT_IMPL:

#define CREATE_OBJECT_IMPL(ObjectType, ...) \
ObjectType* Create##ObjectType(APPLY(PAIR, __VA_ARGS__))  \
{ \
    ObjectType* object = new ObjectType(); \
    [...] \
    object->Init(this, APPLY(STRIP, __VA_ARGS__)); \
    [...] \
    return object; \
}
Run Code Online (Sandbox Code Playgroud)

当然,如果您在visual studio上使用它们,您可能需要一些这些宏的变通方法.当然,更好的解决方案是编写模板化函数.所以你会这样称呼你CreateObject:

ObjectType* obj = CreateObject<ObjectType>(arg1, arg2, arg3);
Run Code Online (Sandbox Code Playgroud)

在C++ 11中,您可以使用如下的varidiac模板:

template< typename ObjectType, typename... Args >
ObjectType* CreateObject(Args... args)
{
    ObjectType* object = new ObjectType();
    [...]
    object->Init(this, args...);
    [...]
    return object;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您的编译器不支持varidiac模板,您可以使用Boost.PP为最多10个参数生成重载(如果需要,还可以生成更多):

#define GENERATE_OBJS_EACH(z, n, data) \
template<class ObjectType, BOOST_PP_ENUM_PARAMS_Z(z, n, class Arg)> \
ObjectType* CreateObject(BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, Arg, arg))  \
{ \
    ObjectType* object = new ObjectType(); \
    [...] \
    object->Init(this, BOOST_PP_ENUM_PARAMS_Z(z, n, arg)); \
    [...] \
    return object; \
}
/* Generate CreateObject template for up to 10 arguments */
BOOST_PP_REPEAT_FROM_TO_1(1, 10, GENERATE_OBJS_EACH, ~)
Run Code Online (Sandbox Code Playgroud)

编辑:下面是使用上述宏在msvc中工作所需的变通方法:

/* This counts the number of args */
#define NARGS_SEQ(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
#define NARGS_MSVC_WORKAROUND(x) NARGS_SEQ x
#define NARGS(...) NARGS_MSVC_WORKAROUND((__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1))

/* This will let macros expand before concating them */
#define PRIMITIVE_CAT(x, y) x ## y
#define CAT_MSVC_WORKAROUND(x) PRIMITIVE_CAT x
#define CAT(x, y) CAT_MSVC_WORKAROUND((x, y))

/* This will call a macro on each argument passed in */
#define APPLY(macro, ...) APPLY_MSVC_WORKAROUND(CAT(APPLY_, NARGS(__VA_ARGS__)), (macro, __VA_ARGS__))
#define APPLY_MSVC_WORKAROUND(m, x) m x
...
Run Code Online (Sandbox Code Playgroud)