Wednesday 10 April 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...