将可变数量的参数传递给构造函数

时间:2012-10-22 23:58:01

标签: c++ c++11

我想完成以下任务:

Entity e;
e.AddComponent<CPosition>(128, 128); //method should instantiate a new CPosition(128,128)
e.AddComponent<CSprite>(some, other, args); //etc

重要的部分是AddComponent方法。它应该尝试使用传递的参数构造泛型类型。我相信C ++ 11的可变参数模板可以将参数转发给构造函数。但是,我还没有访问此功能(VS2010)。

有没有人知道如何做到这一点?

2 个答案:

答案 0 :(得分:7)

写一堆重载,每个重载都有不同的参数。

class Entity
{
public:
 template<typename T>
 void AddComponent() { component_ = new T(); }

 template<typename T, typename T1>
 void AddComponent(T1 t1) { component_ = new T(t1); }

 template<typename T, typename T1, typename T2>
 void AddComponent(T1 t1, T2 t2) { component_ = new T(t1, t2); }

 // etc
 ...
};

答案 1 :(得分:3)

检查boost :: container :: vector :: emplace_back是如何实现的: http://www.boost.org/doc/libs/1_51_0/boost/container/vector.hpp

它使用Boost.Preprocessor自动生成具有不同数量参数的函数。它会生成一些预定义的函数。

结果,您不必手动编写每个重载。相反,您只能编写一次模式。

例如:

#include <boost/preprocessor/iteration/local.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>

struct Entity
{
#define ENTITY_PP_PARAM_LIST(z, n, data) const BOOST_PP_CAT(P, n) & BOOST_PP_CAT(p, n)
#define ENTITY_PP_PARAM_PASS(z, n, data) BOOST_PP_CAT(p, n)

#define BOOST_PP_LOCAL_MACRO(n) \
    template<typename GenericType BOOST_PP_ENUM_TRAILING_PARAMS(n, typename P) > \
    void AddComponent(BOOST_PP_ENUM(n, ENTITY_PP_PARAM_LIST, _)) \
    { \
        something=new GenericType(BOOST_PP_ENUM(n, ENTITY_PP_PARAM_PASS, _)); \
    } \
    /**/

#define BOOST_PP_LOCAL_LIMITS (0, 3)
#include BOOST_PP_LOCAL_ITERATE()
};

预处理后扩展为:

struct Entity
{
    template<typename GenericType  >
    void AddComponent()
    {
        something=new GenericType();
    }

    template<typename GenericType , typename P0 >
    void AddComponent( const P0 & p0)
    {
        something=new GenericType( p0);
    }

    template<typename GenericType , typename P0 , typename P1 >
    void AddComponent( const P0 & p0 , const P1 & p1)
    {
        something=new GenericType( p0 , p1);
    }

    template<typename GenericType , typename P0 , typename P1 , typename P2 >
    void AddComponent( const P0 & p0 , const P1 & p1 , const P2 & p2)
    {
        something=new GenericType( p0 , p1 , p2);
    }
};