class template instantiation

前端 未结 2 853
心在旅途
心在旅途 2020-12-06 01:09

I just read the wiki article about CRTP, and I\'m a little confused about template instantiation.

According to the wiki,

member function bodi

2条回答
  •  攒了一身酷
    2020-12-06 02:07

    You seem to be confusing one thing:

    Instantiation happens during compilation, not during runtime. Hence you can't say "on which line" a class template or a function template was instantiated.

    That said, you're right about the fact that member function templates aren't instantiated together with class templates.

    You could observe it in such a case: You have the following files

    • template.h (defines class A and function A::foo)
    • a.cpp (uses A)
    • b.cpp (uses A and A::foo)

    Then during compilation of a.cpp, only A would be instantiated. However, during compilation of b.cpp, both would be instantiated.

    Because of this, in case A::foo contained some semantically invalid code for a given set of template parameters, you would get compile errors in b.cpp, but not a.cpp.

    I hope that clears things up!

提交回复
热议问题