Tell me, how can you ensure that C ++ templates can be placed in static / dynamic libraries? Now I have to get by tying up the projects to use the written "library" code. Which leads to a long compilation. Or no options?

  • And the question itself is devoid of meaning. What are static / dynamic libraries when it comes to the compile-time tool. - alexlz

1 answer 1

Templates, as the name implies, are a kind of abstract code, which is instantiated and compiled for the current project (obviously, at the time of its compilation).

Thus, as soon as you write #include "header-whatever" in which the template code is located, and then apply some template described in the header-whatever file, then it is at this point that the template code is compiled.

Generally speaking, template compilation time is a separate topic for a conversation, because it all happens for a very long time even in the newest versions of the compiler. The only thing I can note is that according to the results of benchmarks in gcc the 4.6+ branch, the template compilation performance was highly optimized and it does it more easily than, for example, cl or ipp .


Specifically on the issue (albeit rather pointlessly) - you can easily make some non-generic class that will use template methods inside itself, split this class into implementation and declaration ( cpp and header files) and compile this file into a separate static library.

Thus, the template code will be instantiated, and, accordingly, instead of the header file with its code, you will need to link the library with the code of your wrapper.

There is little sense in this action, but the compile time is expected to decrease :)