Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - [bug report] recursive template definition
The dmc compiler crashes on recursive template definitions without any stop condition, without issuing any error message. Example: template<int N> float power(float x) { return power<N-1>(x)*x; } int main() { power<10>(2); } In most cases this isn't a problem, because such source code is incorrect anyway, but it also crashes with the following function used to quickly compute small integer powers (which works well when compiled with Borland C++): template<unsigned N, typename T> inline T power(T x) { return N == 0 ? 1 : (N == 1 ? x : (N == 2 ? x*x : (N == 3 ? x*x*x : (N == 4 ? power<2>(power<2>(x)) : (N == 9 ? power<3>(power<3>(x)) : (N == 12 ? power<3>(power<4>(x)) : (N == 16 ? power<4>(power<4>(x)) : (N == 25 ? power<5>(power<5>(x)) : (N%2 == 0 ? power<2>(power<N/2>(x)) : (N%3 == 0 ? power<3>(power<N/3>(x)) : power<N-1>(x)*x )))))))))); } It doesn't crash when the same function is written this way (but I couldn't figure out how to templatise the type of the function argument when using this solution): template<unsigned N> inline float power(float x) { return N%2 == 0 ? power<2>(power<N/2>(x)) : (N%3 == 0 ? power<3>(power<N/3>(x)) : x*power<N-1>(x)); } template<> inline float power<25>(float x) { return power<5>(power<5>(x)); } template<> inline float power<16>(float x) { return power<4>(power<4>(x)); } template<> inline float power<12>(float x) { return power<3>(power<4>(x)); } template<> inline float power<9>(float x) { return power<3>(power<3>(x)); } template<> inline float power<4>(float x) { return power<2>(power<2>(x)); } template<> inline float power<3>(float x) { return x*x*x; } template<> inline float power<2>(float x) { return x*x; } template<> inline float power<1>(float x) { return x; } template<> inline float power<0>(float x) { return 1; } Sep 09 2004
Szabolcs Horvát wrote:In most cases this isn't a problem, because such source code is incorrect anyway, but it also crashes with the following function used to quickly compute small integer powers (which works well when compiled with Borland C++): template<unsigned N, typename T> inline T power(T x) { return N == 0 ? 1 : (N == 1 ? x : (N == 2 ? x*x : (N == 3 ? x*x*x : (N == 4 ? power<2>(power<2>(x)) : (N == 9 ? power<3>(power<3>(x)) : (N == 12 ? power<3>(power<4>(x)) : (N == 16 ? power<4>(power<4>(x)) : (N == 25 ? power<5>(power<5>(x)) : (N%2 == 0 ? power<2>(power<N/2>(x)) : (N%3 == 0 ? power<3>(power<N/3>(x)) : power<N-1>(x)*x )))))))))); } Sep 09 2004
Daniel James wrote:An alternative is to use a specialised template structure: I hope that helps. Daniel Sep 13 2004
Szabolcs Horvát wrote:class Class { template<unsigned U> struct Member { static void g() {} }; public: void f() { Member<0>::g(); } }; int main() { Class u; u.f(); } Borland C++ 5.5 compiles this without any error while dmc 8.40 stops with test.cpp(5) : Error: '?$Member Class $0 ' is not a member of struct 'Class' --- errorlevel 1 Do you think this is a compiler bug or am I doing something wrong again? Sep 15 2004
|