有没有办法知道模板函数或C++中的类的编译器实例化代码
假设我有以下代码
template < class T> T add(T a, T b){
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
现在我打电话的时候
add<int>(10,2);
Run Code Online (Sandbox Code Playgroud)
我想知道编译器为int特定版本创建的函数.
我正在使用G ++,VC++.如果有些人可以帮我指出编译器选项来实现这一点,将会很有帮助.
希望问题很清楚.提前致谢.
C++模板上下文中的特化和实例化有什么区别.从我到目前为止所读到的内容,以下是我对专业化和实例化的理解.
template <typename T>
struct Struct
{
T x;
};
template<>
struct Struct <int> //specialization
{
//code
};
int main()
{
Struct <int> s; //specialized version comes into play
Struct <float> r; // Struct <float> is instantiated by the compiler as shown below
}
Run Code Online (Sandbox Code Playgroud)
Struct <float>由编译器实例化
template <typename T=float>
struct Struct
{
float x;
}
Run Code Online (Sandbox Code Playgroud)
我对模板实例化和专业化的理解是否正确?
给定模板元程序(TMP),C++编译器是否会生成计算实例化类数的构建统计信息?或者有没有其他方法可以自动获取此号码?因此对于例如对立的因子
#include <iostream>
template<int N> struct fact { enum { value = N * fact<N-1>::value }; };
template<> struct fact<1> { enum { value = 1 }; };
int main()
{
const int x = fact<3>::value;
std::cout << x << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想回到数字3(因为事实<3>,事实<2>,事实<1>被实例化).这个例子当然是微不足道的,但每当你开始使用例如Boost.MPL时,编译时间真的会爆炸,我想知道有多少是由于隐藏的类实例化造成的.我的问题主要是针对Visual C++,但是gcc的答案也会受到赞赏.
编辑:我目前非常脆弱的Visual C++方法是从Stephan T. Lavavej的视频/d1reportAllClassLayout 之一添加编译开关,并在输出文件上执行grep +字数,但它(a)极大地增加了编译时间,并且(b)正则表达式很难100%正确.
c++ templates counter instantiation template-meta-programming