lenerdv
lenerdv

Reputation: 197

initialize a union with template parameter packs

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;

part, which the compiler complains about:

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)...);
      |     ^~~

I tried moving the ellipses around, to no effect. What I'm essentially trying to do is avoid the use of both templates and dynamic dispatch on every frame, but I need templates for initialization. Am I getting some basic syntax wrong? Am I completely missing something? I'm new to the data-oriented paradigm, so I'll gladly take any advice. Minimal reproducible example: https://godbolt.org/z/zGAfXS

Upvotes: 0

Views: 403

Answers (0)

Related Questions