Why do template class member functions have parameter matching and non-member template function don’t? [closed]

  c++, templates

I am currently in a c++ course, and we just learned that if a template function uses the parameter of type size_t, you cannot call that function with an int or const size_t as the compiler will complain. You HAVE to call it with a parameter of type size_t exactly. However, if you have a class with a template member function and a parameter of type size_t, you can call that function with an int no problem. Why is this the case?

For instance

template <class Item>
class bag {
 public:
   int someFunction(Item a, size_t n);
}

and in some other program, we define someFunction, we can call it with an int in our second parameter. However, if we have

template <class Item>
int someFunction(Item a, size_t n) {

}

calling this with an int in our second parameter will result in an error.

Here is the exact screenshot from my book.
enter image description here

And here is them talking about template classes
enter image description here

Source: Windows Questions C++

LEAVE A COMMENT