Wednesday, April 10, 2019

c++ - How to link selected instantiation of templated class/function?

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 
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, foo and foo. 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.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...