https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65133
Bug ID: 65133 Summary: [C++11] Result type deduction proceeds even though argument deduction fails Product: gcc Version: 4.9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: mbianco at cscs dot ch GCC 4.8.0 to 4.9.0 fails in employing SFINAE to find the proper implementation when infinite template recursion is hit. ICPC (15) and CLANG++ (3.7) compile and run the code correctly. #STANDALONE SOURCE #include <iostream> #include <type_traits> template <int I> struct count { using type = typename count<I-1>::type; }; template <> struct count<0> { using type = void; }; template <int I> auto foo(typename std::enable_if<(I>=0)>::type * = nullptr) -> typename count<I>::type /* Substituting count<I>::type with void would make the code to compile.*/ { std::cout << "I>=0" << std::endl; } // Specialization #2 template <int I> void foo(typename std::enable_if<(I<0)>::type * = nullptr) { std::cout << "I<0" << std::endl; } int main() { foo<2>(); // This Works fine foo<-1>(); // This should go the specialization #2 but GCC fails since it goes into counting down 900 times without resolution return 0; } ### COMPILER INVOCATION: g++ -std=c++11 -c main.cpp ### EXPECTED OUTPUT: Successful compile. ### ACTUAL OUTPUT: main.cpp:7:43: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) instantiating ‘struct count<-900>’ using type = typename count<I-1>::type; ^ main.cpp:7:43: recursively required from ‘struct count<-2>’ main.cpp:7:43: required from ‘struct count<-1>’ main.cpp:18:6: required by substitution of ‘template<int I> typename count<I>::type foo(typename std::enable_if<(I >= 0)>::type*) [with int I = -1]’ main.cpp:35:13: required from here main.cpp:7:43: error: invalid use of incomplete type ‘struct count<-900>’ main.cpp:5:8: error: declaration of ‘struct count<-900>’ struct count ^ ### g++ -v OUTPUT: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/apps/dom/gcc/4.9.0/libexec/gcc/x86_64-unknown-linux-gnu/4.9.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: ./configure --prefix=/apps/dom/gcc/4.9.0 --enable-languages=c,c++,fortran --with-mpfr=/apps/dom/gnu/mpfr/3.1.2 --with-mpc=/apps/dom/gnu/mpc/1.0.2 --with-gmp=/apps/dom/gnu/gmp/5.1.3 --with-isl=/apps/dom/gnu/isl/0.12.2 --with-cloog=/apps/dom/gnu/cloog/0.18.1 Thread model: posix gcc version 4.9.0 (GCC)