Search code examples
c++templatesexplicit-specialization

Way to set up class template with explicit instantiations


After asking this question and reading up a lot on templates, I am wondering whether the following setup for a class template makes sense.

I have a class template called ResourceManager that will only be loading a few specific resources like ResourceManager<sf::Image>, ResourceManager<sf::Music>, etc. Obviously I define the class template in ResourceManager.h . However, since there are only a few explicit instantiations, would it be appropriate to do something like...

// ResourceManager.cpp
template class ResourceManager<sf::Image>;
template class ResourceManager<sf::Music>;
...

// Define methods in ResourceManager, including explicit specializations

In short, I'm trying to find the cleanest way to handle declaring and defining a template class and its methods, some of which may be explicit specializations. This is a special case, in which I know that there will only be a few explicit instantiations used.


Solution

  • Yes.
    This is perfectly legittamate.

    You may want to hide the fact that it is templatised behind a typedef (like std::basic_string does) then put a comment in the header not to use the template explicitly.

    ResourceManager.h

    template<typename T>
    class ResourceManager
    {
        T& getType();
    };
    
    // Do not use ResourceManager<T> directly.
    // Use one of the following types explicitly
    typedef ResourceManager<sf::Image>   ImageResourceManager;
    typedef ResourceManager<sf::Music>   MusicResourceManager;
    

    ResourceManager.cpp

    #include "ResourceManager.h"
    
    // Code for resource Manager
    template<typename T>
    T& ResourceManager::getType()
    {
        T newValue;
        return newValue;
    }
    
    // Make sure only explicit instanciations are valid.
    template class ResourceManager<sf::Image>;    
    template class ResourceManager<sf::Music>;