Way to set up class template with explicit instantiations
Asked Answered
U

2

2

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.

Unload answered 20/8, 2010 at 19:44 Comment(0)
R
3

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>;   
Rachitis answered 20/8, 2010 at 20:47 Comment(0)
D
-2

If you require different implementations of the functions, depending on the type, I'd recommend using inheritance instead of templates.

class ResourceManager {
    // Virtual and non-virtual functions.
}

class ImageManager : public ResourceManager {
    // Implement virtual functions.
}

class MusicManager : public ResourceManager {
    // Implement virtual functions.
}
Doriandoric answered 20/8, 2010 at 19:55 Comment(1)
No that isn't what is required. The point is to put the definitions for a class template's functions in a .cpp file that isn't #included in the header (normally you define template functions in the header). This doesn't work unless you explicitly specify the specializations of a class template. In fact, there is only one function that actually needs to be specialized.Unload

© 2022 - 2024 — McMap. All rights reserved.