c++templates

Unwrap c++ templates


I have c++ code with templates. For example

template <class T> 
class Cls {
    T data;
};

typedef Cls<int> TIntCls; // line 100

typedef Cls<float> TFloatCls; // line 200

How can I get definitions for this class without templates, for example:

// line 100 definition:
class Cls_int {
    int data;
}
// line 200 definition:
class Cls_float {
    float data;
}

This classes need me only for help, not for real development, I wouldn`t compile them in real code.

Maybe some flag of compiler (I use gcc) or python-perl-php-other script can do this?

Main difficulty of question is that in project over 100+ H-files with templates, and I recently use template from template class, which use template as template argument from other class. Multiply using keywords also meet in code. So I want to find types of field in some place of code (on line 100 from my example).


Solution

  • You can use cppinsights to get an idea of what that would look like. Here's what I input:

    template <class T> 
    class Cls 
    {
      T data;
    };
    
    int main()
    {
      Cls<int> cls;
    }
    

    Here's the output I get:

    template<class T>
    class Cls
    {
      T data;
    };
    
    /* First instantiated from: insights.cpp:8 */
    #ifdef INSIGHTS_USE_TEMPLATE
    template<>
    class Cls<int>
    {
      int data;
      public: 
      // inline Cls() noexcept = default;
    };
    
    #endif
    
    int main()
    {
      Cls<int> cls;
      return 0;
    }