mercredi 3 août 2016

No matching function call for template specialization of const char*

I am trying to learn the template specialization for C++ language, coming across this situation I can't solve myself. Codes below:

#include <vector>
#include <iostream>
#include <string>
#include <cmath>

using namespace std;
size_t count(const vector<string> &, const char* const &);

template <typename T>
size_t count(const vector<T>& v, const T& find) {
  size_t c = 0;
  for (auto ele : v) {
    if (ele == find)
      ++c;
  }
  return c;
}

template <>
size_t count(const vector<double> &v, const double &find) {
  size_t c = 0;
  for (auto ele : v) {
    if (abs(ele - find) < 0.0001)
      ++c;
  }
  return c;
}

template <>
size_t count(const vector<const char*> &v, const char* const &find) {
  size_t c = 0;
  for (auto ele : v) {
    if (strcmp(ele, find) == 0)
      ++c;
  }
  return c;
}

size_t count(const vector<string> &v, const char* const &find) {
  size_t c = 0;
  for (auto ele : v) {
    if (strcmp(ele.c_str(), find) == 0)
      ++c;
  }
  return c;
}

int main() {
  vector<double> vd{0.1, 0.1, 0.2};
  cout << count(vd, 0.1) << endl;
  vector<string> vs{"I", "you", "I"};
  cout << count(vs, "I") << endl;
  vector<const char*> vc{"I", "you", "I"};
  cout << count(vc, "I") << endl;
  return 0;
}

Compiling this code, cout << count(vc, "I") << endl; will report error about not finding a matching function call.

However, I deliberately declare a specialization for this function call, namely template <> size_t count(const vector<const char*> &v, const char* const &find)

What is wrong here?

Thank you.

Aucun commentaire:

Enregistrer un commentaire