Is it possible to use the C++11 initializer_list to assemble a recursively defined class such as Foo, below, using constexpr constructors:
template <size_t N>
struct Foo {
constexpr Foo(int x, Foo<N-1> f) : x(x), xs(xs) {}
int x;
Foo<N-1> xs;
};
template <> struct Foo<0> {};
I can initialise a Foo<3> using:
int main(int argc, char *argv[])
{
Foo<3> a = Foo<3>(1,Foo<2>(2,Foo<1>(3,Foo<0>())));
return 0;
}
It would be nice to use Foo<3> a = {1,2,3} instead. If there was a constexpr tail function in initializer_list I think it should work.
Yes, in a kind of round-about way, effectively unpacking and repacking the initializer list to a more suited format. However, there is a better (imho) way: Variadic templates.
Output as expected:
Note that I chose
1as the base case, as it simply makes more sense.