使用模板参数包初始化联合

I'm writing an engine with ECS and the data-oriented model. I'm trying to avoid inheritance and dynamic dispatch to avoid trashing the cache with every update() call. What I came up with was this:

struct transformComponent {
    const unsigned short id;
    vecShort p;

    transformComponent(unsigned short id, short x, short y): p(x, y), id(id) {}
    transformComponent(unsigned short id): p(0, 0), id(id) {}
};

struct physicsComponent {
    const unsigned short id;
    unsigned short mass;
    double invmass;
    vecShort v, a, f;

    physicsComponent(unsigned short id, unsigned short mass):
        id(id), mass(mass), invmass(1/mass) {}
    physicsComponent(unsigned short id): id(id) {}
};

//...

struct component {
    enum compType {transform, physics, behavior, collision, rendering};

    union {
        transformComponent  tra;
        physicsComponent    phy;
        //...
    };

    template<typename ...argtypes>
    component(compType type, unsigned short id, argtypes... args) {
        switch(type) {
            case transform:
                tra(id, std::forward<argtypes>(args)...);
                break;
            case physics:
                phy(id, std::forward<argtypes>(args)...);
                break;
            //...
        }
    }
};

Then I have a room class (the manager) that holds all the systems and components, through memory pools. The problem is the

template<typename ...argtypes>
    component(compType type, unsigned short id, argtypes... args) {
        switch(type) {
            case transform:
                tra(id, std::forward<argtypes>(args)...);
                break;

部分,编译器抱怨:

src\inc/components.h: In instantiation of 'component::component(component::compType, short unsigned 
int, argtypes ...) [with argtypes = {}]':
src\inc/components.h:94:71:   required from here
src\inc/components.h:57:5: error: no match for call to '(transformComponent) (short unsigned int&)'
   57 |     tra(id, std::forward<argtypes>(args)...);
      |     ^~~

我尝试移动椭圆,没有任何效果。我实质上想做的是避免在每个帧上同时使用模板和动态调度,但是我需要用于初始化的模板。我的基本语法错误吗?我完全错过了什么吗?我是面向数据的范例的新手,所以我很乐意接受任何建议。

评论