Bug 56449

Summary: operator-> in a template class can lead to infinite template instantiation
Product: gcc Reporter: Vasily Sukhanov <basil>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: jakub
Priority: P3    
Version: 4.6.3   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Vasily Sukhanov 2013-02-25 22:38:36 UTC
Code example:

template<class T> struct Loop { Loop<T*> operator->(); };
Loop<int> i, j = i->something;

The compiler doesn't diagnose infinite Loop<> instantiation. Even CLI option ftemplate-depth=n doesn't help.
Comment 1 Jonathan Wakely 2013-02-25 22:42:12 UTC
Both 4.7 and 4.8 exit with a sensible diagnostic:

a.cc:2:19: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) instantiating ‘Loop<T*> Loop<T>::operator->() [with T = int************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************]’
a.cc:2:19:   recursively required from ‘Loop<T*> Loop<T>::operator->() [with T = int]’
a.cc:2:19:   required from here

a.cc:2:19: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) instantiating ‘struct Loop<int*************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************>’
a.cc:2:19:   recursively required from ‘Loop<T*> Loop<T>::operator->() [with T = int]’
a.cc:2:19:   required from here

a.cc:2:19: error: invalid use of incomplete type ‘struct Loop<int*************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************>’
a.cc:1:26: error: declaration of ‘struct Loop<int*************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************>’
Comment 2 Jakub Jelinek 2013-02-25 22:45:10 UTC
Dup of PR49118.

*** This bug has been marked as a duplicate of bug 49118 ***