简体   繁体   中英

C++ template specialization

Hi I am implementing template specialization with C++, where I would like a function foo to do something if the input (and output) types are float and double, but would like foo to act differently for int.

I seem to be doing something wrong. Can you please provide me some pointers? Thanks a bunch!

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;
}

So what I was missing was <int, int> . complete code:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM