Infinite recursion in variadic template instantiation trying to build arbitrary depth tree-like structure

Viewed 939

I'm doing some experimentation with variadics and I've stumbled into an issue I can't figure out the solution of - basically I'm trying to build a tree with components of arbitrary data types - here is some code:

template <class A, class B>
struct SeqExpression
{
    const A & first;
    const B & then;
};

template <class A, class B>
SeqExpression<A,B>
make_seq(const A & a, const B & b)
{
    return {a,b};
}

template <class A, class B, class ...T>
auto
make_seq(const A & first, const B & second, T ...rest) -> decltype(make_seq(make_seq(first,second),rest...))
{

    return make_seq(make_seq(first,second),rest...);
}

Then I try:

auto x = make_seq("X","Y",'z');

But GCC(4.7) tells me:

error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting ‘template<class A, class B, class ... T> decltype (make_seq(make_seq(first, second), rest ...)) make_seq(const A&, const B&, T ...) [with A = SeqExpression<char [2], char [2]>; B = char; T = {}]’
recursively required by substitution of ‘template<class A, class B, class ... T> decltype (make_seq(make_seq(first, second), rest ...)) make_seq(const A&, const B&, T ...) [with A = SeqExpression<char [2], char [2]>; B = char; T = {}]’
required by substitution of ‘template<class A, class B, class ... T> decltype (make_seq(make_seq(first, second), rest ...)) make_seq(const A&, const B&, T ...) [with A = char [2]; B = char [2]; T = {char}]’

It seems to me though that it should be solvable !

make_seq("X","Y") has type SeqExpression< char[2],char[2] > so make_seq(make_seq("X","Y"),'z') has type SeqExpression< SeqExpression< char[2],char[2] >,char >

and it seems relatively non-loopy to me.

Any thoughts?

1 Answers
Related