2

Lets say, I have 3 classes, that provides same functions. They are unrelated in class hierarchy but they are implemented in such way to be similar to eachother.

  • class A
  • class B
  • class C

For example purposes lets assume that A, B and C are arrays that hold some integers, but every class holds it in different way. Every class provides its iterator class and class_name::begin() and class_name::end(). Now I have some function template foo that increments every element of the array.

template <typename T>
void foo(T & a)
{
    for(auto & element : a)
        element++;
}

But lets say that foo has enormously big body, that works for every type T as A, B, or C. How can I tell compiler:

I will use only foo<A>, foo<B> and foo<C>. Let me move my function to .cpp file and link it.

The reason I am asking this is because templates are used to shorten code, that works for more than one compile-time-resolvable context. I don't want to write the same function inside .cpp file 3 times.

Poeta Kodu
  • 1,120
  • 8
  • 16

0 Answers0