生成已扩展模板的C ++代码 [英] Generate C++ code where templates have been expanded

查看:92
本文介绍了生成已扩展模板的C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以使用C ++编译器来生成C ++代码,其中所有模板均已扩展为程序所需的实例化。该代码必须在编译过程中的某个时刻存在,尽管默认情况下可能不是ASCII形式,但是在我看来,将其转换回可读的C ++代码并不难。

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.

编辑:我最感兴趣的是一种解决方案,该解决方案将生成实际的C ++代码(至少可以编译,并且理想情况下也可读),而不仅是实例化的类和函数的概述。 。对于使复杂的模板代码更具可读性(例如,用于调试),或者在代码需要由常规C ++编译器(可能不(完全)支持模板)以外的软件处理的情况下,这似乎是有用的功能。

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.

推荐答案

模板是语言的一部分,而不是某些预处理程序的传递,它们是由编译器处理的,就像其他代码一样。

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

在Visual Studio 2012中无法执行此操作(属性->配置属性-> C / C ++->预处理程序->预处理为文件-不会给您扩展模板)。

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).

g ++具有调试选项束。我会尝试 -fdump-class-hierarchy (未经测试)。

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

这篇关于生成已扩展模板的C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆