Explicit instance - when is it used?

After a few weeks off, I try to expand and expand my knowledge of templates using the Templates book - The Complete Guide by David Vandevoord and Nikolai M. Josuttis, and what I'm trying to understand at this point is the explicit creation of templates.

Actually, I have no problem with the mechanism as such, but I cannot imagine a situation in which I would like or would like to use this function. If anyone can explain this to me, I will be more than grateful.

+63
c ++ templates
Feb 28. '10 at 13:17
source share
3 answers

Directly copied from https://docs.microsoft.com/en-us/cpp/cpp/explicit-instantiation :

You can use explicit instance creation to instantiate a template class or function without actually using it in your code. Since this is useful when you create library files (.lib) that use templates for distribution, unused template definitions are not placed in object files (.obj).

(For example, libstdc ++ contains an explicit instantiation std::basic_string<char,char_traits<char>,allocator<char> > (which is std::string ), so every time you use the std::string functions, the same function code no need to copy to objects. The compiler only needs to reference (link) to libstdc ++.)

+41
Feb 28 '10 at 13:20
source share

If you define a template class that you want to use for only a few explicit types.

Place the template declaration in the header file just like a regular class.

Place the template definition in the source file as a regular class.

Then, at the end of the source file, an instance of only the version in which you want to be available is explicitly created.

Silly example:

 // StringAdapter.h template<typename T> class StringAdapter { public: StringAdapter(T* data); void doAdapterStuff(); private: std::basic_string<T> m_data; }; typedef StringAdapter<char> StrAdapter; typedef StringAdapter<wchar_t> WStrAdapter; 

Source:

 // StringAdapter.cpp #include "StringAdapter.h" template<typename T> StringAdapter<T>::StringAdapter(T* data) :m_data(data) {} template<typename T> void StringAdapter<T>::doAdapterStuff() { /* Manipulate a string */ } // Explicitly instantiate only the classes you want to be defined. // In this case I only want the template to work with characters but // I want to support both char and wchar_t with the same code. template class StringAdapter<char>; template class StringAdapter<wchar_t>; 

home

 #include "StringAdapter.h" // Note: Main can not see the definition of the template from here (just the declaration) // So it relies on the explicit instantiation to make sure it links. int main() { StrAdapter x("hi There"); x.doAdapterStuff(); } 
+54
Feb 28 2018-10-18T00
source share

It depends on the compiler model - apparently, there is a Borland model and a CFront model. And then it also depends on your intention - if you are writing a library, you can (as indicated above) explicitly create specialized specializations.

The GNU c ++ page discusses the models here https://gcc.gnu.org/onlinedocs/gcc-4.5.2/gcc/Template-Instantiation.html .

+1
Mar 28 '18 at 18:39
source share



All Articles