使用外部函数的 C++ 模板

标签 c++ templates external

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/

相关文章:

azure - Azure 上的 Kubernetes : assign different external IP to different services

c++ - 使用com端口在cpp中通过串口发送数据

带模板的 C++ Lambda

regex - Grails-从外部属性文件中读取正则表达式

wpf - ListViewItem 自定义模板 : ContentPresenter stays empty

c++ - 具有引用参数的重载函数模板

javascript - 通过外部接口(interface)接收复杂的 JavaScript 值

c++ - Qt:更改 centralWidget 时崩溃

c++ - 如何在 OpenCV Mat 中保留固定的缓冲区大小?

c++ - '从 some_type** 到 const some_type** 的无效转换'