Infinite recursion in an instance of a variation template trying to create an arbitrary tree structure of depth

I am experimenting with variators, and I came across a problem, I can’t understand the solution - basically I try 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 that it should be solvable!

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

and for me it looks relatively non-loopback.

Any thoughts?

+5
source share
1 answer

, , ( ). , ; - :

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

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

Variadic ; , , .


, , -, g++ . , :

template <int n, class ...T> struct mst_helper;
template <class A, class B>
struct mst_helper<2, A, B> { typedef SeqExpression<A, B> type; };
template <int n, class A, class B, class ...T>
struct mst_helper<n, A, B, T...> {
    typedef typename mst_helper<n - 1, SeqExpression<A, B>, T...>::type type; };
template <class ...T>
struct make_seq_type { typedef typename mst_helper<sizeof...(T), T...>::type type; };

template <class A, class B, class C, class ...T>
typename make_seq_type<A, B, C, T...>::type
make_seq(const A & first, const B & second, const C &third, T ...rest)
{
    return make_seq(make_seq(first,second), third, rest...);
}

g++ - 4.7.1, , , struct using.

+3

All Articles