2012-03-08 47 views
0

我有問題,極長的時間編譯我的C++代碼,我使用一些模板功能從外部庫。加速模板函數編譯

例子:

//fun.h 
template <class T> 
T fun(T in){ 
... 
} 

//main.cpp 
#include fun.h 
class A{...}; 
int main(){ 
A a,b; 
... 
b=fun<A>(a); //this line causes the long compilation time, because fun is really complicated 
... 
} 

我想在單獨的頭文件在某種程度上定義新的功能

funA := fun<A> 

和預編譯。因此,每個我改變main.cpp的時候我沒有再一次的建立

fun<A> 

。但我不知道該怎麼做。我認爲,隨着類,您只需把

typedef class<A> classA; 

預編譯的頭,和你做。但如何用功能來做到這一點?

+0

多少時間是*極長*?代碼真的有多複雜?它是否實例化許多其他模板? – 2012-03-08 22:49:57

+0

在GCC中,您可以嘗試使用預編譯頭文件。 – 2012-03-08 22:57:45

回答

1

wrap_fun.h

A funA(A a); 

wrap_fun.C

#include "wrap_fun.h" 
#include "fun.h" 

A funA(A a) 
{ 
    return fun(a); // Should deduce type automatically. 
}