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

前端 未结 8 1919
死守一世寂寞
死守一世寂寞 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:08

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

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

    Is translated to

    template auto add(X x, Y y) {
      return x + y;
    }
    
    /* First instantiated from: insights.cpp:9 */
    #ifdef INSIGHTS_USE_TEMPLATE
    template<>
    double add(int x, double y)
    {
      return static_cast(x) + y;
    }
    #endif
    
    
    int main()
    {
      return static_cast(add(10, 2.5));
    }
    

提交回复
热议问题