Template Compilation

后端 未结 7 1985
鱼传尺愫
鱼传尺愫 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:54

    The main point here is that compiler does not treat a template definition until it meets a certain instance of the template. (Then it can proceed, I guess, like it have a usual class, which is a specific case of the template class, with fixed template parameters.)

    The direct answer to your question is: Compiler generates machine code from users c++ code, I think this is wat is meant here by word "generate code".

    The template declaration must be in header file because when compiler compiles some source, which use template it HAVE only header file (included in source with #include macro), but it NEED whole template definition. So logical conclusion is that template definition must be in header.

提交回复
热议问题