How to expand/“preprocess” C++ template code

后端 未结 3 1367
心在旅途
心在旅途 2020-12-08 09:47

To properly debug complex macros in C++ I usually run the preprocessor on them in order to see exactly what the resulting code looks like.

Is there a similar way to

3条回答
  •  臣服心动
    2020-12-08 10:19

    The CLang compiler features an option -emit-ast which dumps the Abstract Syntax Tree used to represent the parsed language. The various instantiations of the template will be represented.

    The AST is represented both in memory and in xml version, so you can:

    • just use the XML output
    • parse it, then produce some C++ code
    • create a Rewriter tool (supported directly in CLang) and consume the AST itself

    For most code inspections (including checking the overloads selected) I have found that actually reading the XML output (well, grepping through it) was sufficient for my needs.

提交回复
热议问题