Can we see the template instantiated code by C++ compiler

前端 未结 8 1889
死守一世寂寞
死守一世寂寞 2020-11-30 20:09

is there a way to know the compiler instantiated code for a template function or a class in C++

Assume I have the following piece of code

template &l         


        
相关标签:
8条回答
  • 2020-11-30 21:07

    I think c++ Insights is what you want.

    0 讨论(0)
  • 2020-11-30 21:08

    Now there is an on-line tool which does this for you: https://cppinsights.io/ For example, this code

    template<class X, class Y> auto add(X x, Y y) {
      return x + y;
    }
    
    int main()
    {
      return add(10, 2.5);
    }
    

    Is translated to

    template<class X, class Y> auto add(X x, Y y) {
      return x + y;
    }
    
    /* First instantiated from: insights.cpp:9 */
    #ifdef INSIGHTS_USE_TEMPLATE
    template<>
    double add<int, double>(int x, double y)
    {
      return static_cast<double>(x) + y;
    }
    #endif
    
    
    int main()
    {
      return static_cast<int>(add(10, 2.5));
    }
    
    0 讨论(0)
提交回复
热议问题