samedi 3 octobre 2015

C++ variadic template - limit number of args

I have a variadic template class with set function, that fills interal array:

template <size_t Dim>
class Vector
{
public:

    void SetValueTemplate(size_t index)
    {
        return;
    }

    template <typename X0, typename ...Xn>
    void SetValueTemplate(size_t index, X0 x0, Xn... xn)
    {
        val[index] = x0;
        SetCenterValueTemplate(index + 1, xn...);
    }

    template <typename ...X0>
    void SetValue(X0... t0)
    {
        SetValueTemplate(0, t0...);
    }

private:
    double val[Dim];
};

Problem is, that I can call this

Vector<3> v;
v.SetValue(0, 1, 2, 4, 5);

and it compiles correctly. Can I limit this to not compile? I can use static_assert, but is it possible without it?

Aucun commentaire:

Enregistrer un commentaire