jeudi 4 juin 2020

Let function take pointer to template object without specifying template parameters

This is my situation. I have a class, a template class with an int template parameter, similar to this.

template <int n>
class example
{
  bool some_method()
  {
     return n > 1;
  }
}

Now, what I want to do is write a function that takes a pointer to an object of this class and uses it. For the things I have to do, there is no need to know the template parameter, the code would look the same for all template parameters. Something like:

bool function(example* part1, bool part2)
{
     return part1->some_method() && part2;
}

But obviously compiler complains for the lack of template parameters. Any chance I implement such a function without writing a templae function? the example is small, but real life example would be way more verbose and convoluted.

Working on Gcc 4.8.1 with c++11 enabled, https://godbolt.org/z/EXdCe9 example on goldbolt to show the error.

Aucun commentaire:

Enregistrer un commentaire