使用显式实例化设置类模板的方法

发布于 2024-09-15 07:36:20 字数 690 浏览 8 评论 0原文

在提出这个问题并阅读了大量有关模板的内容之后,我想知道以下类模板设置是否有意义。

我有一个名为 ResourceManager 的类模板,它只会加载一些特定资源,例如 ResourceManagerResourceManager< /code> 等。显然我在 ResourceManager.h 中定义了类模板。然而,由于只有几个显式实例化,是否适合做类似的事情...

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

// Define methods in ResourceManager, including explicit specializations

简而言之,我试图找到最干净的方法来处理声明和定义模板类及其方法,其中一些可能明确专业化。这是一个特殊情况,我知道只会使用一些显式实例化。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蛮可爱 2024-09-22 07:36:20

是的。
这是完全合法的。

您可能想隐藏它在 typedef 后面进行模板化的事实(如 std::basic_string 所做的那样),然后在标头中添加注释以不要显式使用模板。

资源管理器.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;

资源管理器.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>;   

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>;   
快乐很简单 2024-09-22 07:36:20

如果您需要不同的函数实现,根据类型,我建议使用继承而不是模板。

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

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

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

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文