Template Compilation

后端 未结 7 1984
鱼传尺愫
鱼传尺愫 2020-11-29 07:25

I have what could possibly be a dumb question, but I\'m just not understanding my book\'s explanation for it.

It says \"When the compiler sees the definition of a t

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 07:57

    When you create a function and compile it, the compiler generates code for it. Many compilers will not generate code for static functions that are not used.

    If you create a templated function and nothing uses the template (such as std::sort), the code for the function will not be generated.

    Remember, templates are like stencils. The templates tell how to generate a class or function using the given template parameters. If the stencil is not used, nothing is generated.

    Consider also that the compiler doesn't know how to implement or use the template until it sees all the template parameters resolved.

提交回复
热议问题