如何扩展/“预处理” C ++模板代码 [英] How to expand/"preprocess" C++ template code

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

问题描述

为了在C ++中正确地调试复杂的宏,我通常对它们运行预处理器,以便准确地看到结果代码的样子。

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.

preprocess模板代码?

Is there a similar way to "preprocess" template code?

推荐答案

一种方法(依赖于编译器)是在每个编译器步骤之后使用转储。我写了一个小程序:

One way (compiler-dependent) is to use dumping after each compiler step. I wrote a small program:

template<class T>
T square(T n)
{
    return n * n;
}

int main(void)
{
    square<int>(3);
    square<float>(3.0);
}

那么:

g++ -fdump-rtl-all test.cc

给我一堆文件。看看(在我的case)test.cc.218.dfinish:

This get me a bunch of files. Take a look at (in my case) test.cc.218.dfinish:

;; Function int main() (main)
;; Function T square(T) [with T = int] (_Z6squareIiET_S0_)
;; Function T square(T) [with T = float] (_Z6squareIfET_S0_)

这篇关于如何扩展/“预处理” C ++模板代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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