How do templates work, are they always built-in?

I can understand how this works if they are built-in. But if it is not, how does it work? Do all object files get their own copy of, for example, a function template?

+6
c ++ templates inline
source share
6 answers

Templates will be embedded in the standard inline value, which is more related to the definition rule than to the actual code attachment. That is, the linker will not complain if the template functions are defined in more than one translation unit, it just selects one (beware: random, current compilers do not complain if you provide different template definitions in different translation units!) And leave this in the final binaries.

Now, as in all other inline functions, the compiler can decide that it is a good idea to actually avoid calling the function and embed the function in the place of the call, or it can determine that it is not such a good idea (big function, some compilers are not built in functions with nested loops ... whatever the reason), and then it wonโ€™t actually insert the code.

+4
source share

It depends on the compiler, but each one I looked at creates a function that can then be called using the parameters of the replaceable template to generate code for each parameter.

as a (very) simple example:

 template <typename T> T Max(T a, T b) { return a > b ? a : b; } 

when Max<int> and Max<float> called and not nested, the compiler generates (they are specially designed to prevent other problems):

 int Max(int a, int b) { return a > b ? a : b; } float Max(float a, float b) { return a > b ? a : b; } 

Then it gets stuck at the beginning of the object, and then refers to it, then this is done for some embedded lines (in MSVC)

+2
source share

It depends. Some of the most popular implementations generate a copy of the instance code in each object file that starts the instance, and expects the linker to throw away all but one. Other compilers use a kind of storage where instances are stored; if the instance is already present, the compiler does not bother regenerating it. This solution is much faster and uses less disk than the first solution, but it is also much more difficult to be right. (The compiler must generate a new instance not only if it is missing, but if any of the files depends on the instance being changed.)

+2
source share

Depends on the implementation.

But as a rule, yes, every object file gets a copy of every extended function that they use. And then the linker notices this during the link and ensures that only one copy of the function will be placed in the final executable

+1
source share

Templates are really very advanced MACROS (#define)

Options

are replaced at compile time with the passed values. Really great concept, and also implemented very well.

0
source share

Templates are a complete language for yourself. They are fully completed, but the "program" works at compile time. These are code factories that replace the type of an object at compile time and collect classes, functions, etc. At compile time. So you can think of it as a type of safe, C ++ compatible massive preprocessing language. The output is pure C ++ code, which can then be processed by the compiler in the same way as everything else.

Compilers usually ignore inline, since very few programmers can really know when it is better, and those who have not left the assembly.

0
source share

All Articles