3

given Variadic templates data structure as below ( code from Eli Bendersky's website):

template <class... Ts> struct tuple {};

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

  T tail;
};

we are able to define something like

tuple<double, uint64_t, const char*> t1(12.2, 42, "big");

Then how to define a template structure that could accept following signature?

Foo<tuple<int,double>, tuple<double,int,long> ...,  tuple<std::string>> foo;

I think it would be like this:

template<tuple<class... Ts>... Tuples>
struct VariadicTuples {};

but it cannot compile. is that because the class in the Variadic templates cannot be Variadic templates? If so, How to make this work?

qqibrow
  • 2,942
  • 1
  • 24
  • 40
  • 1
    In your example with `Foo`, you're not passing the template itself but an instanciation thereof. `template struct Foo;` is enough for this to compile. – Quentin Aug 26 '15 at 18:51

1 Answers1

4

You simply cannot write that, syntactically. What would Ts hold in that situation? It would be different for each type in Tuples so it wouldn't really be usable.

What you can do instead is take advantage of Columbo's bool_pack trick:

template <bool...> struct bool_pack;

template <bool... v>
using all_true = std::is_same<bool_pack<true, v...>, bool_pack<v..., true>>;

To simply static_assert that all the Tuples are, in fact, tuples and then have VariadicTuples be a simple variadic class template:

template <typename > struct is_a_tuple : std::false_type { };
template <typename... T> struct is_a_tuple<tuple<T...>> : std::true_type { };

template <typename... Tuples>
struct VariadicTuples {
    static_assert(all_true<is_a_tuple<Tuples>::value...>::value, "!");
};
Community
  • 1
  • 1
Barry
  • 286,269
  • 29
  • 621
  • 977