samedi 2 juillet 2016

Parameter pack expansion via the constructor

Is there any way to accomplish what I am trying to do:

struct Base
{
    template<typename T>
    void PeelOff(T const& t);

    template<>
    void PeelOff<int>(int const& i)
    {
        i_ = i;
    }

    template<>
    void PeelOff<float>(float const& f)
    {
        f_ = f;
    }

    Base() { }

    template<typename T1, typename... T>
    Base(T1 const& arg1, T&&... args)
    { 
        PeelOff(arg1);
        Base(args...);
    }

private:
    int i_;
    float f_;
};

struct Foo : public Base
{
    template<typename... T>
    Foo(T&&... args)
        : Base(args...)
    { }
};

int main() { Foo f(1, 22.2f); return 0; }

This has a problem in that recursion terminator ends up being the default ctor itself and that blows away the values I extracted.

I tried many different tweaks and I am not quite getting what I want.

Aucun commentaire:

Enregistrer un commentaire