vendredi 13 novembre 2020

Universal Reference and constness

This C++ code doesn't compile, you get an error "candidate function template not viable: 1st argument ('const int32_t' (aka 'const int')) would lose const qualifier"

I know I can solve this by adding an overload for Func(const T& value), but I'm curious to learn why this doesn't compile?

template <typename T> 
void Func(T&& value)
{
  // Do stuff
}

struct Obj
{
  int32_t Id{};
};

int main(int argc, char* argv[])
{
  const Obj i{};
  Func<int32_t>(i.Id);
}

Aucun commentaire:

Enregistrer un commentaire