首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为了为各种模板实例化提供类型防御而使用宏是否是使用它们的一个有效原因?

为了为各种模板实例化提供类型防御而使用宏是否是使用它们的一个有效原因?
EN

Stack Overflow用户
提问于 2014-09-23 04:32:29
回答 2查看 87关注 0票数 0

我正在编写一个小型C++ ECS框架。为了提高性能,大多数框架类型都是可变模板,需要实体组件的编译类型信息。因此,最终用户必须做一些如下的事情:

代码语言:javascript
复制
#include "./Components.hpp"
#include <framework/..>

class ExampleSystem : public fw::System<Component1, Component2, Component3....>
{
    ....
    bool registrationCondition(fw::Entity<Component1, Component2, Component3....> entity)
    {
        ....
    }

    ....

};

等。

所有模板实例化都需要相同的模板参数包。然而,正如你所看到的,这是丑陋和乏味的。我想知道在库中使用宏来提供类型防御的一般意识是什么。

例如:

代码语言:javascript
复制
#include "./Components.hpp"
//used by the framework to provide convenient typedefs. Obviously should be guarded and put in another file
#define FW_COMPONENTS Component1, Component2, Component3, Component4, ....
#include <framework/..>

class ExampleSystem : public fw::System
{
    ....
    bool registrationCondition(fw::Entity entity)
    {
        ....
    }
    ....
};

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-23 09:22:16

不需要宏,你可以这样做:

代码语言:javascript
复制
template <typename ...Ts> struct typedef_helper
{
    using system = fw::System<Ts...>;
    using entity = fw::Entity<Ts...>;
    // And so on.
};

然后

代码语言:javascript
复制
using my_typedef_helper = typedef_helper<Component1, Component2, Component3>;
using my_system = typename my_typedef_helper::system;
using my_entity = typename my_typedef_helper::entity;
// And so on.
票数 1
EN

Stack Overflow用户

发布于 2014-09-23 06:27:21

您不需要宏来避免冗余的类型列表:

代码语言:javascript
复制
template <typename... Args>
class System
{
protected:
    typedef Entity<Args...> Entity_type;
};

然后,用法:

代码语言:javascript
复制
class ExampleSystem : public System<Component1, Component2, Component3>
{
    bool registrationCondition(Entity_type entity)
    {
    }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25986953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档