mercredi 14 novembre 2018

Using fields stored in variadic data structure as method arguments

I have a variadic data structure, each "layer" containing one field.

How can use all the fields stored in the structure as arguments to a function or a constructor?

template <class... Ts> class Builder {};

template <class T, class... Ts>
class Builder<T, Ts...> : public Builder<Ts...> {
public:
    Builder(T t, Ts... ts) : Builder<Ts...>(ts...), tail(t) {}

    Result build() {
      // want to use tail, Builder<Ts...>::tail, etc.
      // as ctor or function arguments without multiple specializations
    }

private:
    const T tail;
};

In general, I want to be capable of doing something like this:

Builder<int, string, int> b1{10, "aaa", 20};
Result r1 = b1.build(); // should invoke Result's constructor (int, string, int)

Builder<int> b2{10};
Result r2 = b2.build(); // should invoke Result's constructor (int)

Aucun commentaire:

Enregistrer un commentaire