Explicit template instantiation - when is it used?

前端 未结 4 1285
一整个雨季
一整个雨季 2020-11-22 11:14

After few weeks break, I\'m trying to expand and extend my knowlege of templates with the book Templates – The Complete Guide by David Vandevoorde and Nicolai

4条回答
  •  无人共我
    2020-11-22 11:51

    Directly copied from https://docs.microsoft.com/en-us/cpp/cpp/explicit-instantiation:

    You can use explicit instantiation to create an instantiation of a templated class or function without actually using it in your code. Because this is useful when you are creating library (.lib) files that use templates for distribution, uninstantiated template definitions are not put into object (.obj) files.

    (For instance, libstdc++ contains the explicit instantiation of std::basic_string,allocator > (which is std::string) so every time you use functions of std::string, the same function code doesn't need to be copied to objects. The compiler only need to refer (link) those to libstdc++.)

提交回复
热议问题