class template instantiation

耗尽温柔 提交于 2019-11-27 22:55:16

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!

Sebastian Mach

With class templates, the rule of thumb is that only those members are instantiated which are actually used.

If you want complete instantiation, C++ offers explicit instantiation (however, usually you don't; the fact that not every bit is fully instantiated means that your template class is even more generic as it lowers the requirements on T, note that syntax checking and lookup of non-dependent types (stuff that is not dependent on T) still happens).

You will find a more complete answer here: Template instantiation details of GCC and MS compilers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!