Menu

CppCheck Max Template recursion limit seems to be not properly implemented

2024-02-08
2024-02-08
  • Peter Friedland

    Peter Friedland - 2024-02-08

    Hi,

    in our project we use heavily generated code and therefore a lot of template classes. We have observed that we come across the limit of max template instantiations and analyzed that this is not totally true,

    CppCheck is counting all instantiations of a template inside a template (i.e. the Inner<n> inside the example - without this the error doesn't happen) as recursive instantiations of that template.</n>

    That is it doesn't differentiate between something like

    template<int N> struct recursive { recursive<N - 1> r; };
    

    and

    template<int N> struct Unrelated {};
    template<int N> struct NotRecursive { Unrelated<N> u; }; 
    

    instead counting both as "recursive" templates (although in the first case it is counting the "recursive" instantiations, in the later case it is counting Unrelated instantiations.

    It would be helpful if cppcheck would improve as this would not lead for us to the limit and we can remove workarounds from our side.

     
  • CHR

    CHR - 2024-02-08
    template<int N> struct recursive { recursive<N - 1> r; };
    recursive<100> r;
    
    foo.cpp:1:36: information: TemplateSimplifier: max template recursion (100) reached for template 'recursive<-1>'. You might want to limit Cppcheck recursion. [templateRecursion]
    template<int N> struct recursive { recursive<N - 1> r; };
                                       ^
    

    I don't get a warning for

    template<int N> struct Unrelated {};
    template<int N> struct NotRecursive { Unrelated<N> u; }; 
    NotRecursive<1000> n;
    Unrelated<1000> un;
    

    Am I missing something?

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.