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

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

    If you want to see the assembly output, use this:

    g++ -S file.cpp
    

    If you want to see some (pseudo) C++ code that GCC generates, you can use this:

    g++ -fdump-tree-original file.cpp
    

    For your add function, this will output something like

    ;; Function T add(const T&, const T&) [with T = int] (null)
    ;; enabled by -tree-original
    
    return  = (int) *l + (int) *r;
    

    (I passed the parameters by reference to make the output a little more interesting)

提交回复
热议问题