C ++模板专业化

je

嗨,我正在用C ++实现模板特化,我想让函数foo在输入(和输出)类型为float和double的情况下做一些事情,但希望foo对int采取不同的行动。

我似乎做错了事。您能给我一些指导吗?谢谢一堆!

template <typename typeA, typename typeB>
typeA foo(const typeB *pt) {
  // do something;
} 

template float foo<float, float>(const float *pt);
template double foo<double, double>(const double *pt);

template<>
int foo(const int *pt) {
  // do something different for int;
}
je

所以我想念的是<int, int>完整的代码:

template <typename typeA, typename typeB>
typeA foo(const typeB *pt) {
  // do something;
} 

template float foo<float, float>(const float *pt);
template double foo<double, double>(const double *pt);

template<>
int foo<int, int>(const int *pt) {
  // do something different for int;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章