简体   繁体   中英

How can I specialize a static templated class?

I'm trying to create a static class to store and retrieve assets such as sound buffers and textures. The way this is implemented is identical for both of these cases, so I created a templated static class and tried to specialize them to store and retrieve objects of the respective types. Here is my idea:

template<typename Asset>
class AssetHandler
{
public:
    static const Asset& retrieve_asset(const std::string& asset_file_path)
    {
    //retrieve the asset from m_assets, or load it in...
    }

private:
    static std::unordered_map<std::string, Asset> m_assets;
};

using ImageHandler = AssetHandler<Image>;
using SoundHandler = AssetHandler<Sound>;

Trying to make calls such as ImageHandler::retrieve_asset("image.png") results in a linker error complaining about undefined references to AssetHandler{Image}::m_assets . I've thought about just using inheritance here, but is that really necessary? How can I get what I already have to work?

Add after the class:

template<typename Asset>
std::unordered_map<std::string, Asset> AssetHandler<Asset>::m_assets;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM