Generate C++ code where templates have been expanded

给你一囗甜甜゛ 提交于 2019-12-09 00:55:08

问题


I was wondering if there is a way to use a C++ compiler to just produce C++ code where all templates have been expanded to the instantiations that are required by the program. This code must exist at some point in the compilation process, although probably not in ASCII form by default, but it should not be too difficult to convert it back to readable C++ code in my opinion.

EDIT: I am mostly interested in a solution that will produce actual C++ code (which at least compiles and ideally is also readable), not just an overview of classes and functions that are instantiated. This seems like a useful feature for making complex templated code more readable (e.g. for debugging), or in case the code needs to be processed by software other than a regular C++ compiler, which may not (fully) support templates.


回答1:


Templates are part of the language, not some pre-processor pass - they are processed by the compiler, just like other code.

There is no way to do this in Visual Studio 2012 (Property -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocess to a File - won't give you expanded templates).

The g++ has a bunch of debug options. I would try -fdump-class-hierarchy (not tested).




回答2:


I think "Templator" might help: Demo at CppCon2015, Slides




回答3:


One way would be to compile your program in debug and then dump the symbols. All the compiled templates must have their symbol defined.

g++ -g foo.cpp -o foo.o

Ofcourse you would missed inlined functions, but this can be suppressed with

g++ -g -fno-inline-functions foo.cpp -o foo.o

To get the symbols use:

objdump -t --demangle a.out


来源:https://stackoverflow.com/questions/33233944/generate-c-code-where-templates-have-been-expanded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!