mercredi 21 mars 2018

std::tuple, automatic constructor order

Consider the following code snippet

struct MachineGun {
    explicit  MachineGun (int a);
};

struct Turret {
    explicit  Turret (char b);
};

struct Cannon {
    explicit  Cannon (std::string c);
};


template<typename... Ts>
struct Enemy {
    template<typename...Args>
    Enemy(Args&&... args)
        : device_list {std::forward<Args>(args)...}
    {

    }

private:
    std::tuple<Ts...> device_list;
};

In the above constructor std::forward with the args unpack matches the correct constructor of the type inside the tuple but I cannot figure out how to try to make this match regardless of order of the types or parameters.

Hypothetically the code below represent the same thing: "Give me an enemy with a machinegun, turret and cannon"

Enemy<MachineGun,Turret,Cannon> e1(1,'1',std::string("1"));
Enemy<MachineGun,Turret,Cannon> e2("3",14,'5');
Enemy<Turret,MachineGun,Cannon> e3(std::string("14"), 5, '33');

But how do I actually enable this? e1 will compile as the args list matches the type list order, both e2 and e3 should represent the same thing but how do I make my compiler understand this?

Aucun commentaire:

Enregistrer un commentaire