作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在math_functions.h 文件中编写了一个模板函数,它使用另外两个模板函数(add 和 mul):
template <typename Dtype>
Dtype mulvadd(Dtype* pa, Dtype* pb, int size, Dtype c)
{
Dtype result = Dtype(0);
for (int k = 0; k < size; k++)
{
result = add<Dtype>(result, mul<Dtype>(pa[k],pb[k]));
}
result = add<Dtype>(result, c);
return result;
}
int16_t mulv_int16(int16_t* pa, int16_t* pb, int size);
在 math_functions.cpp 中,我对 add 和 mul 有不同的特化,但也有针对 int16_t 类型的 mulvadd 特化:
#include <stdint.h>
#include <fix16.h>
template<> float add<float>(float a, float b) { return a + b;}
template<> float mul<float>(float a, float b) { return a*b;}
template<> int16_t add<int16_t>(int16_t a, int16_t b) { return fix16_sadd(a, b); }
template<> int16_t mul<int16_t>(int16_t a, int16_t b) { return fix16_smul(a, b); }
#ifndef FIXMATH_NO_32BIT
template<> int16_t mulvadd<int16_t>(int16_t* pa, int16_t* pb, int size, int16_t c)
{
return c + mulv_int16(pa, pb, size);
}
#endif
int16_t mulv_int16(int16_t* pa, int16_t* pb, int size)
{
....
}
我也写了一个简单的测试程序:
#include "math_functions.h"
int main(int argc, char ** argv) {
int16_t av[4] = {241,134};
int16_t bv[4] = {-28, 7};
int16_t res = mulvadd<int16_t>(av, bv, 2, 0);
printf("res=%f\n", (double)res);
}
这段代码编译没有任何错误,但奇怪的是,当我调用 mulvadd 时,调用的函数是 h 文件中定义的默认模板,而不是 cpp 文件中的专用版本。发生这种情况的原因是什么?
最佳答案
编译器在使用 main
函数编译文件时不会知道特化。它只知道头文件中的内容。
在头文件中声明特化,以便编译器了解它们。
关于c++ - 函数模板特化被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39961380/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
正如您在 this travis.yml 中看到的那样文件,我的代码依赖于一些第三方库,我在构建项目之前将它们安装在远程系统上。 Travis 每次推送提交时都会下载并构建这些库,这可以避免吗?我的意
我是一名优秀的程序员,十分优秀!