我正在编写一个小型C++ ECS框架。为了提高性能,大多数框架类型都是可变模板,需要实体组件的编译类型信息。因此,最终用户必须做一些如下的事情:
#include "./Components.hpp"
#include <framework/..>
class ExampleSystem : public fw::System<Component1, Component2, Component3....>
{
....
bool registrationCondition(fw::Entity<Component1, Component2, Component3....> entity)
{
....
}
....
};等。
所有模板实例化都需要相同的模板参数包。然而,正如你所看到的,这是丑陋和乏味的。我想知道在库中使用宏来提供类型防御的一般意识是什么。
例如:
#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)
{
....
}
....
};提前谢谢。
发布于 2014-09-23 09:22:16
不需要宏,你可以这样做:
template <typename ...Ts> struct typedef_helper
{
using system = fw::System<Ts...>;
using entity = fw::Entity<Ts...>;
// And so on.
};然后
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.发布于 2014-09-23 06:27:21
您不需要宏来避免冗余的类型列表:
template <typename... Args>
class System
{
protected:
typedef Entity<Args...> Entity_type;
};然后,用法:
class ExampleSystem : public System<Component1, Component2, Component3>
{
bool registrationCondition(Entity_type entity)
{
}
};https://stackoverflow.com/questions/25986953
复制相似问题