2015-09-01 38 views
1

我正在写可以使用函子的cuda内核,这些函数是作为模板参数传递的。例如:有没有办法避免在很多文件中实例化大量模板?

template<typename Functor> void myKernel(float arg1, float* arg2, Functor f) { 
    // Do stuff that will involve f 
} 

这些函子在头文件,我包括在每个CPP文件中定义,并且为每一个我必须与所有的函子实例化所有的内核:

template<> myKernel<Add>(float, float*, Add) 
template<> myKernel<Sub>(float, float*, Sub) 

这是很多代码重复,我们必须记住为每个新的函子添加一个新行。 有没有办法定义所有这一切?

+0

预处理宏? – Amit

+0

您是否知道显式实例化? https://msdn.microsoft.com/en-us/library/by56e477.aspx –

+0

我希望有一个不涉及marcros的解决方案。 – Pafnouti

回答

0

只需在头文件中添加instanciations,而不必每次都记住指定它们。

1

看看extern template declarations.

大约有模板的extern一些微妙的细节,特别是14.7.2.10:

除了内联函数和类模板特, 显式实例声明有效果抑制它们引用的实体的隐式实例化。

这意味着,下面将只抑制其他翻译单位的非联成员函数f的实例,但不为克:

template<typename T> class A { 
public: 
    void g() {} // inline member function 
    void f(); 
}; 

template<typename T> void A::f() {} // non-inline 
相关问题