vendredi 7 août 2020

Why specialization and template does not match?

I am quite new in c++ templates. This is the sample code I have written to test std::enable_if_t staff. But it does not compile with following error:

No function template matches function template specialization 'print'
Candidate template ignored: couldn't infer template argument 'T'

What am I doing wrong?

#include <string>
#include <type_traits>

class IPoint
{
public:
   IPoint()
      : x(0), y(0)
   {}

   IPoint(int xValue, int yValue)
      : x(xValue), y(yValue)
   {}

public:
   int x;
   int y;
};

namespace utils
{
   template<typename T>
   typename std::enable_if_t<true, T> print(const std::string& s) 
   {
      return 0;
   }

   template<>
   inline IPoint print(const std::string& s)
   {
      return IPoint(0, 0);
   }
}

Aucun commentaire:

Enregistrer un commentaire