how does templates work, are they always inlined?

后端 未结 6 1520
谎友^
谎友^ 2020-12-20 19:59

I can understand how it work if they are inlined. But if they are not, how does it work? does all object files get their own copy of for example the function template anyway

相关标签:
6条回答
  • 2020-12-20 20:34

    It depends. Some of the more popular implementations do generate a copy of the instantiation code in each object file which triggers an instantiation, and count on the linker to throw out all but one. Other compilers use some sort of repository, where instantiations are stored; if the instantiation is already present, the compiler doesn't bother regenerating it. This solution is significantly faster and uses less disk than the first solution, but it's also a lot harder to get right. (The compiler has to generate a new instantiation not only if one isn't present, but if any of the files the instantiation depends on have changed.)

    0 讨论(0)
  • 2020-12-20 20:36

    it's implementation dependent.

    But commononly, yes each object file gets a copy of each expanded function they use. And then the linker notices this at link time and ensures that only one copy of the function gets put in the final executable file

    0 讨论(0)
  • 2020-12-20 20:36

    Templates are really very very advanced MACROS (#define)

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

    0 讨论(0)
  • 2020-12-20 20:39

    Templates will be inlined in the standard meaning of inline, which is more related to the One Definition Rule than to actual code inlining. That is, the linker will not complain if the template functions are defined in more than one translation unit, it will just pick one (beware: random one, current compilers do not complain if you provide different definitions of the template in different translation units!) and leave that in the final binary.

    Now, as with all other inline functions, the compiler can decide that it is a good idea to actually avoid the function call and inline the function at the place of call, or it might determine that it is not such a good idea (big function, some compilers don't inline functions with nested loops... whatever the reason) and then it will not perform the actual code inlining.

    0 讨论(0)
  • 2020-12-20 20:40

    Depends on the compiler, but every one I've looked at creates a function that is then callable using the substitued template parameters to generate the code for each varient.

    as a (very) simple example:

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

    when invoked as Max<int> and Max<float> and not inlined, the compiler generates (they are decorated in a special way however, 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;
    }
    

    This is then stuck at the start of the object and then referenced, then same is done for some inlines too (in MSVC)

    0 讨论(0)
  • 2020-12-20 20:42

    Templates are a complete language unto themselves. They are Turing complete, but the "program" runs at compile time. They are code factories that replace the object type at compile time and assemble classes, functions, etc. at compile time. So you can think of it as a type safe, C++ compatible massive preprocessing language. The resulting output of the execution is pure C++ code that can then be treated by the compiler the same as it does everything else.

    Compilers generally ignore inline as very few programmers can really know when it's best and those who do have not left assembly.

    0 讨论(0)
提交回复
热议问题