Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it advisable to expose a templated class of a Library?

I am developing a Library for Matrix Calculations in C++. For this I wanted to use Templates. After doing a bit of Template Meta Programing, I realized that I would end up in exposing my implementations in the Templatise Matrix Class.Is there any way to obfuscate the template class implementation in the header file when you expose that particular template class ? If yes, then how is it done ?

like image 592
Atul Avatar asked Dec 06 '22 21:12

Atul


2 Answers

I will answer from the customer perspective.

When I need to use a library, and integrate it in my code, I expect to see the source code.

It is not because I wish to rip it out from the author... It is not because I am a lawless and irrespective hacker...

It is, simply, because:

  • code is documentation, and seeing the implementation of a method will help me compensate for the lack of it, or perhaps better understand what it meant (*)
  • for debugging, the ability to step down into library's code is invaluable
  • for developing, it is just so much easier if I can compile the code myself, in various flavors (with and without instrumentation, aka gcov, with and without debug symbols, etc...)

I don't ask for the code to be free, I am perfectly fine with the code being licensed, and I'll scrupulously follow the license terms, I just ask for the code to be available.

Frankly, if I have the choice between two libraries, and one does not expose its code, I'll lean toward the other, unless the performance/correctness difference is really important.

(*) In C++, Boost has libraries that I consider fundamentally broken in this regard. The code is riddled with compilers work-around, which makes it very difficult to read. Nevertheless, I use them because they're just awesome.

like image 111
Matthieu M. Avatar answered Dec 25 '22 06:12

Matthieu M.


As templating means that the implementation of the class/function is created compile-time (needs to make a new implementation for each new type) I cannot see how you could hide the code. The only way would be to hide your templates in a precompiled library and only expose interfaces to predefined types. That would lose the template functionality though...

like image 28
Orka Avatar answered Dec 25 '22 04:12

Orka