Now I have a new question. Is it possible to have a template-Funktion in a dll-File?
It seems not to be possible because the datatype has to be defined at compiler timer.
|
Context is everything in answering this question.
If the types are declared in the DLL, sure. The template can fully instantiate a type defined in the DLL's code. So, in your example, if a type is defined in dll_header.h, and the template code is defined there, then that template can be instantiated in dll_function.cpp and called from ithin dll_function.cpp.
Further, if that type is used in the code loading and consuming the dll, then that code can call the functions of the template code built in the dll.
To be clear, this would mean those functions would have to be exported under those conditions, or the template code would have to be entirely inline in the includes used to interface to the dll.
You are correct to assume, however, that if those types are not exposed to the code consuming the dll, and if the template code is not exported where function calls are created, then the code consuming the dll could not reach the template code.
To put this another way, fitting a slightly different context, say a custom container similar to a stack is created as template code, and is entirely placed in a header. Both the DLL code and the code consuming the DLL include that header. They both instantiate the template. There may be code built inside the DLL for that stack template, and there will be duplicate code built in the consumer of the DLL. The two will not mix or conflict if the DLL doesn't export the duplicates.
Most modern use of templates are "header only" code. If both the consumer of the DLL and the DLL itself include that template, there may be duplicate code, but that code should be compatible. There can be memory allocation questions about what is allocated inside the DLL and then passed to the consumer, or vica versa, and if there were any static or global data involved, that could be an issue (there would likely be duplicates there, too).