How to publish header files with template implementations?

We are creating a set of libraries with a public API that should be used by various third parties. Some of the libraries are pure C, so obviously they have a C-style header with functions and structure definitions and a corresponding library. They are allright.

Some of the libraries are written using moderately complex C ++ (for older compilers), so we implemented some form of the famous idiom pimpl. This is also normal.

On the other hand, a significant portion of the header files are C ++ using highly boilerplate code. Knowing Why templates can only be implemented in the header file? , but also do not want to reveal too many implementation details for the eyes that should not see them, we reorganized them strongly to exclude as many internal details as possible and have only the really necessary bits ... and still a significant amount of code remains.

So, this puzzles me: is there a preferred way to distribute header files that mostly contain templates? What are the good methods, best approaches, tips and tricks?

+5
source share
1 answer

Look at your C ++ compiler header files for inspiration. The C ++ Standard Library is filled with templates, and you will usually find all the template code in the headers.

Having said that if certain templates are intended to be used with a small number of possible classes (or values) as template parameters, you have the option to explicitly create templates within the library itself, leaving only the open template ads visible in the header files.

Using the simpler pre-C ++ 11 script as an example, the C ++ library usually provides an implementation of std::basic_string only for std::basic_string<char> and std::basic_string<wchar_t> ; and leave a bunch of template code inside the library itself, with a simple template declaration std::basic_string visible in the header files.

+4
source

Source: https://habr.com/ru/post/1201726/


All Articles