https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66153
Bug ID: 66153 Summary: Internal compiler error in nested template function Product: gcc Version: 5.1.0 Status: UNCONFIRMED Severity: major Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: paboyle at ph dot ed.ac.uk Target Milestone: --- Created attachment 35543 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=35543&action=edit Preprocessed source Peters-MacBook-Pro:gcc5_debug pab$ g++-5 -std=c++11 broken.cc -o broken ' Internal compiler error: Error reporting routines re-entered. broken.cc: In substitution of 'template<int N, class obj> Container<decltype (function<N>(arg.data[0]))> function(const Container<obj>&) [with int N = 1; obj = <missing>]': broken.cc:43:101: recursively required by substitution of 'template<int N, class obj> Container<decltype (function<N>(arg.data[0]))> function(const Container<obj>&) [with int N = 1; obj = <missing>]' broken.cc:43:101: required by substitution of 'template<int N, class obj> Container<decltype (function<N>(arg.data[0]))> function(const Container<obj>&) [with int N = 1; obj = <missing>]' broken.cc:45:33: Abort trap: 6 template<int N,class obj> auto function(const Container<obj> & arg)-> Container<decltype(function<N>(arg.data[0]))> ^ g++-5: internal compiler error: Abort trap: 6 (program cc1plus) Please submit a full bug report, with preprocessed source if appropriate. See <https://github.com/Homebrew/homebrew-versions/issues> for instructions. ============== #include <vector> #include <complex> #include <type_traits> #include <iostream> typedef std::complex<double> ComplexD; template <class T> class TypeMapper { public: enum { NestLevel = T::NestLevel }; }; template<> class TypeMapper<ComplexD> { public: enum { NestLevel = 0 }; }; template<class obj> class Container { public: std::vector<obj> data; Container(int size) : data (size){}; }; template<class obj> class Recursive { public: enum { NestLevel = TypeMapper<obj>::NestLevel + 1}; obj internal; }; template<int N,class obj,typename std::enable_if<N==obj::NestLevel >::type * = nullptr > auto function(const obj &arg)-> obj { std::cout<<"Leaf "<<obj::NestLevel<<std::endl; return arg; } template<int N,class obj,typename std::enable_if<N!=obj::NestLevel >::type * = nullptr > auto function(const obj &arg)-> obj { std::cout<<"Node "<<obj::NestLevel<<std::endl; obj ret; ret.internal=function<N>(arg.internal); return ret; } template<int N,class obj> auto function(const Container<obj> & arg)-> Container<decltype(function<N>(arg.data[0]))> { Container<decltype(function<N>(arg.data[0]))> ret(arg.data.size()); for(int ss=0;ss<arg.data.size();ss++){ ret.data[ss] = function<N>(arg.data[ss]); } return ret; } int main(int argc,char **argv) { Container<Recursive<Recursive<ComplexD> > > array(10); Container<Recursive<Recursive<ComplexD> > > ret(10); ret = function<1>(array); } ============== For reference, works on Intel 15, Clang++ multiple versions. Peters-MacBook-Pro:gcc5_debug pab$ icpc -std=c++11 broken.cc -o broken Peters-MacBook-Pro:gcc5_debug pab$ clang++ -std=c++11 broken.cc -o broken Peters-MacBook-Pro:gcc5_debug pab$ clang++ --version Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn) Target: x86_64-apple-darwin14.1.0 Thread model: posix Peters-MacBook-Pro:gcc5_debug pab$ icpc --version icpc (ICC) 15.0.3 20150408 Copyright (C) 1985-2015 Intel Corporation. All rights reserved. Output with both icpc and clang++ is: Peters-MacBook-Pro:gcc5_debug pab$ ./broken Node 2 Leaf 1 Node 2 Leaf 1 Node 2 Leaf 1 Node 2 Leaf 1 Node 2 Leaf 1 Node 2 Leaf 1 Node 2 Leaf 1 Node 2 Leaf 1 Node 2 Leaf 1 Node 2 Leaf 1