mercredi 18 septembre 2019

Function in template class vs template function

When we have a function template like this:

template <class T>
void push_back(T&& type)
{

}

It is perfectly legal, to call it like this:

push_back(1);

and like this:

int i = 0;
push_back(i);

But, when I have a template class:

template <class Type>
class List
{
public:
 void push_back(Type&& value)
 {
 }
};

I cannot call it like this:

  List<int> list_int;
  int i = 42;
  list_int.push_back(i);

because I am getting the folllowing:

 error: rvalue reference to type 'int' cannot bind to lvalue of type 'int'

Would someone explain to me why, please?

Aucun commentaire:

Enregistrer un commentaire