Variable templates + generic lambdas for std::map

烂漫一生 提交于 2019-12-05 11:11:34
template<typename T>
std::map<int, T> storage;

This is a declaration, much like e.g. template<typename T> class foo;. As we need one anyway, we would benefit from making it a definition as well:

template<typename T>
std::map<int, T> storage {};

This doesn’t get rid of the linker errors however, suggesting there’s an outstanding bug with implicit instantiation. To convince ourselves, we can trigger instantiation in various ways:

  • explicitly, which would look like

    // namespace scope, same as storage
    template /* sic */ std::map<int, int>         storage<int>;
    template           std::map<int, std::string> storage<std::string>;
    
  • implicitly, by adding the following inside main

    storage<int>.size();
    storage<std::string>.size();
    

in either case taming the linker.

What you attempted is explicit specialization, which does indeed get around the issue although using a different mechanism.

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