mercredi 28 septembre 2022

couldn't infer template argument - do I need to use explicit template parameters?

template <int I, typename T> struct Wrap {
  T internal;
};

template <int I, typename T>
Wrap<I, T> DoStuff(int z) { return Wrap<I, T>{(T)z}; }

class Wrapped {
public:
// Working
  Wrap<1, int> GetInt() { return DoStuff<1, int>(1); }
  Wrap<2, long> GetLong() { return DoStuff<2, long>(2); }

// Not working
  Wrap<3, char> GetChar() { return DoStuff(3); }
};

Try it online

Why is the third function failing to resolve the template argument? I thought the compiler would try to match the template definition with the return type of the function. Is this working in c++14 or any newer versions?

Aucun commentaire:

Enregistrer un commentaire