简体   繁体   中英

C++ Templates compilation

I am failing to answer questions related to templates. Basically how templates are compiled by the compiler. I googled but did not find answers. Can somebody help me

Read a book, like C++ Templates - The Complete Guide by David Vandevoorde and Nicolai M. Josuttis. Beside that it explains how to use them it also does give some insight on how they are implemented.

Templates themselves are not compiled, particular instantiations of templates are. Templates can be instantiated by simply being used or by being explicitly instantiated

Eg given a function template:

template<class T> void f() {}

This is just a template for a function, which you can use:

f<int>(); // compiler will instantiate a concrete f<int>()

... leading to that particular instantiation being compiled. Alternatively you can explicitly instantiate it:

template void f<int>();

The original template however is never compiled, it is just used to create concrete instances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM