how does templates work, are they always inlined?

后端 未结 6 1625
谎友^
谎友^ 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: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.

提交回复
热议问题