1
这里是一个小例子,误差为模板类
#include <iostream>
template<typename T>
struct MyClass{
T value;
};
template<typename T, template<typename> class Class>
void foo(Class<T>& myClass){
myClass.value=0;
};
int main(){
MyClass<double> myclass;
foo<double, MyClass<double>>(myclass);
return 0;
}
该代码将无法编译,并给出错误
error: no instance of function template "foo" matches the argument list
argument types are: (MyClass<double>)
foo<double, MyClass<double>>(myclass);
我为什么要写作的原因一个函数是我想写一个在CPU和GPU之间传输数据的函数。函数看起来像
template<typename Scalar, template<typename > class Host,
template<typename> class Device>
void copyFromHostAsync(const Host<Scalar> &host, Device<Scalar> &device, cudaStream_t stream) {
assert(host.rows() == device.rows());
assert(host.cols() == device.cols());
cudaMemcpyAsync(device.getPtr(), host.getPtr(), sizeof(Scalar) * host.rows() * host.cols(),
cudaMemcpyHostToDevice, stream);
}
我想使用模板类作为参数,以便底层标量类型是相同的。
欢迎任何帮助。
确实是这样的先生!非常感谢你! –