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
+3
A:
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.
wilx
2010-08-02 10:52:39
+3
A:
Templates themselves are not compiled, particular instantiations of templates are. Templates can be instantiated by simply being used or by being explicitly instantiated
E.g. 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.
Georg Fritzsche
2010-08-02 10:55:13
@georg but if we do not instantiate any concrete instance, and if there are any syntax error in template compiler throws errors. Why is this
siri
2010-08-02 16:20:53
@siri Templates are not compiled, but they are parsed by the compiler before any instantiations takes place, which means they must be syntactically correct.
anon
2010-08-02 16:29:40