The first case works without any errors:

#include <tuple>

template<typename Type>
class A { 
};


template<typename... Types>
class B : A<B<Types...> > { 
   std::tuple<Types...> t;
};

Now, altering the tuple to hold a struct derived from Types also works:

template<typename T>
struct C_M { };

template<typename... Types>
class C : A<C<Types...> > { 
   std::tuple<C_M<Types>...> t;
};

However, declaring the struct inside, like this:

template<typename... Types>
class D : A<D<Types...> > { 
   template<typename T>
   struct M { };

   std::tuple<M<Types>...> t;
};

Does not, as GCC 4.4.3 gives this error:

cxxtest.cpp:24: error: template instantiation depth exceeds maximum of 500 (use
-ftemplate-depth-NN to increase the maximum) instantiating ‘class A<D<int,
char, bool> >’
cxxtest.cpp:24:   instantiated from ‘D<int, char, bool>’
cxxtest.cpp:28:   instantiated from ‘D<int, char, bool>’
cxxtest.cpp:28:   instantiated from ‘D<int, char, bool>’
cxxtest.cpp:28:   instantiated from ‘D<int, char, bool>’
(repeats another 496 times)

As shown, the error does have something to do with the tuple declaration on
recursive instantiation of class D in the declaration. This error comes out of
nowhere, as D is not even mentioned by the tuple declaration.

What is pretty much impossible to determine from this error (but is implied by
the correct operation of C above) is that the tuple declaration is missing a
typename:

template<typename... Types>
class E : A<E<Types...> > {
   template<typename T>
   struct M { };

   std::tuple<typename M<Types>...> t;
};

I've no idea how GCC is arriving at the infinite recursion, but improved
analysis that could allow GCC to determine the actual error (or a near
approximation thereof, as for other missing typename errors) would be very
nice. Here is the complete test code with the erroneous line commented out:

#include <tuple>

template<typename Type>
class A {
};


template<typename... Types>
class B : A<B<Types...> > {
   std::tuple<Types...> t;
};


template<typename T>
struct C_M { };

template<typename... Types>
class C : A<C<Types...> > {
   std::tuple<C_M<Types>...> t;
};


template<typename... Types>
class D : A<D<Types...> > {
   template<typename T>
   struct M { };

// std::tuple<M<Types>...> t;
};


template<typename... Types>
class E : A<E<Types...> > {
   template<typename T>
   struct M { };

   std::tuple<typename M<Types>...> t;
};

int main() {
   B<int, char, bool> b;
   C<int, char, bool> c;
   D<int, char, bool> d;
   E<int, char, bool> e;
   return 0;
}

This compiles cleanly, except for unused variables warnings. I do not have a
GCC 4.5 or newer installation to compare against.


-- 
           Summary: C++0x Variadic Templates: Infinite template recursion
                    rather than an error message
           Product: gcc
           Version: 4.4.3
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Clint dot Smullen at gmail dot com
 GCC build triplet: x86_64-linux-gnu
  GCC host triplet: x86_64-linux-gnu
GCC target triplet: x86_64-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45698

Reply via email to