c++templatesdll

Conesequences of Headers Mis-Matching A Library?


I've come to realize that there are times whilst developing a C++ library where including implementation code in the .h files associated with the library becomes unavoidable. An example of such a time is when developing a template of a method, class, etc.

I already understand that a template of something transforms what that something represents from a concrete implementation of itself to instructions for the compiler to follow when creating concrete implementations behind usages of the template'd something. In the context of library development, this tells me that moving implementation code of a template method, class, etc. out from the file that declares the template isn't possible or else those who link the library will not have the instructions necessary to compile concrete implementations behind uses of the template (among other reasons one cannot have implementation code for a template in a different file than the declaration of the template).

With the above example specifically in mind, what are the consequences to the one linking a library in the case that one or more .h files that come with the library do not exactly match the contents of the library itself?

I have already accepted that there is simply no way I can give templates of things to those who link the library without including the full implementation code in the .h files that declare these templates since it just doesn't make sense otherwise. I should also add that in the greater context of what I am trying to achieve I cannot concrete implement all variations of the templates in this example in question because I want the linker of the library to be able to concrete implement their own variations on these templates. That said, I just want to know what would happen if someone linking the library attempted to tamper with the .h files. Would the compile fail? Would linking fail? Would the build succeed and just produce a ton of runtime errors? I'd test myself in an MRE but I'm not confident enough with library development to know if any results from that MRE are themselves the result of my own ineptitude.

Thanks for reading!


Solution

  • what are the consequences to the one linking a library in the case that one or more .h files that come with the library do not exactly match the contents of the library itself?

    It might simply crash and burn in ways that are impossible to foresee. Yes, that includes you deleting all the user's files.

    It might work, if the only difference in ABI is between symbols you are not using.

    It might not even produce code that can be linked, and so you might not even be able to produce a binary.

    So, it depends.