Way to set up class template with explicit instantiations

不打扰是莪最后的温柔 提交于 2019-12-11 02:36:08

问题


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.


回答1:


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>;   



回答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.
}


来源:https://stackoverflow.com/questions/3534336/way-to-set-up-class-template-with-explicit-instantiations

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