Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewing compiler expanded code - C++

I learned that compiler will expand macros while compiling. Templates are also expanded at the compile time. Is there any way to see this expanded code? I am compiling using Visual Studio 2008.

any thoughts?

like image 808
Navaneeth K N Avatar asked Jan 20 '09 12:01

Navaneeth K N


2 Answers

The compiler doesn't actually do any of the macro expansion. That is the task of the pre-processor. It all appears as one step, but the compiler actually forks out to a separate pre-processor tasks and traps the output for you.

Templates are not "expanded" at compile time. They are instantiated on use during compile. The difference is that the compiler immediately generates object code for the template; there's no intermediate source code that comes out. You can't look at the instantiated template code as source, it's dumped out as assembly when it's needed.

If you have GCC you can also call the pre-processor directly using 'cpp' with the right arguments (mostly include paths and command line macro definitions). Others have answered for MSVC.

like image 173
Adam Hawes Avatar answered Sep 18 '22 12:09

Adam Hawes


Note that /E in VC++ only expands preprocessor statements (that is, #include, #ifdef, #define etc.)

I am not aware of any modern compiler that allows to expand templates.

like image 40
peterchen Avatar answered Sep 19 '22 12:09

peterchen