【发布时间】:2013-04-28 02:25:23
【问题描述】:
我一直在阅读一些关于 templates 的网页。
我看到了,templates 是这样使用的:
template <typename T>
T func(T a) {...}
因此,它可以灵活地为不同类型的变量使用相同的代码。 而且,我们可以使用似乎只适用于类的专业化,就像:
template <> class A<int> {....}
但是我没有找到这样的用法:
template<int N, bool isVertical, bool isFirst, bool isLast>
static void filter(int bitDepth, Pel const *src, int srcStride,
short *dst, int dstStride, int width,
int height, short const *coeff);
它是这样称呼的:
filter<N, false, true, true>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
在这段代码中,模板被赋予了真实和绝对类型,恕我直言,我们可以在过滤器的参数列表中添加另外四个参数而不是使用模板。
那么,为什么要这样使用模板呢?
【问题讨论】:
-
我相信这是为了确保它们是编译时值(即常量表达式)。