0

I am trying to invoke a template function, but get the error:

error: no matching member function for call to 'getRecord'
  return getRecord(player, myVoidPtr,
         ^~~~~~~~~
./file.h:90:15: note: candidate template ignored: could not match 'function<type-parameter-0-0 (char *)>' against
      '(lambda at file.cc:55:5)'
  const char *getRecord(const T& item, void *filePtr, std::function<T(char*)> parseFn);

Here is my implementation:

const char *getPlayerRec(const std::string& player) {
  return getRecord(player, myVoidPtr,
    [](const char *record) {
      return string(record);
    }
  );
}

template <typename T>
const char *getRecord(const T& item, void *filePtr, std::function<T(char*)> parseFn) {
  // (elided)
}

What have I misunderstood?

dylhunn
  • 989
  • 2
  • 8
  • 25
  • What does it say past that in the error message? The compiler also tells you why it couldn't match the given possibility – Justin Apr 04 '18 at 22:21
  • The compiler can't deduce `T` from the lambda. You need some way of ruling out the `function` parameter from deduction. – David G Apr 04 '18 at 22:25
  • Indeed, explicitly specifying the type at the call site `getRecord(...)` causes the compiler to generate much more useful error messages (in this case, I had a missing `const` elsewhere.) – dylhunn Apr 04 '18 at 22:26

0 Answers0