C++ 是否允许在模板规范实现中使用外部函数?我想做的是为 BLAS/LAPACK 制作一个包装器(CBLAS 不适合我,我需要一些不在其中但在外部 BLAS 中的函数),有点像这样
extern "C" {
void saxpy_(int* n, const float* const sa, const float* const sx, int* incx, float* sy, int* incy);
void daxpy_(int* n, const double* const sa, const double* const sx, int* incx, double* sy, int* incy);
}
namespace matrix {
template<class T>
void ax_plus_y(const T a, const Matrix<T, Dynamic, Dynamic>& x, Matrix<T, Dynamic, Dynamic>& y) {
y += a * x;
}
template<>
void ax_plus_y<float>(const float a, const Matrix<float, Dynamic, 1>& x, Matrix<float, Dynamic, 1>& y) {
int n = x.size();
int incx = x.innerStride();
int incy = y.innerStride();
saxpy_(&n, &a, x.data, &incx, y.data(), &incy);
}
template<>
void ax_plus_y<double>(const double a, const Matrix<double, Dynamic, 1>& x, Matrix<double, Dynamic, 1>& y) {
int n = x.size();
int incx = x.innerStride();
int incy = y.innerStride();
daxpy_(&n, &a, x.data, &incx, y.data(), &incy);
}
}
这只是我需要的一小部分,但这可能吗?我真的不能只使用一个函数,因为我需要对每种数据类型进行不同的外部调用。
附言Matrix
来自 Eigen,应该与概念无关。
编辑:澄清一下,这就是 g++ 抛出的内容:
error: template-id ‘ax_plus_y<float>’ for ‘void matrix::ax_plus_y(float, const Eigen::Matrix<float, -0x00000000000000001, 1>&, Eigen::Matrix<float, -0x00000000000000001, 1>&)’ does not match any template declaration
最佳答案
当然可以,为什么不呢?
关于使用外部函数的 C++ 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6835711/