简体   繁体   English

“命名”模板类实例化

[英]“Naming” template class instantiations

In my game engine, I've created a pretty funky component-based entity system which allows me to define entity types as a combination of components, specified as template arguments, eg: 在我的游戏引擎中,我创建了一个非常时髦的基于组件的实体系统,该系统允许我将实体类型定义为组件的组合,指定为模板参数,例如:

typedef Entity<Locateable, Collidable, Renderable /* etc */ > SomeEntity;

This works fine, until I need to uniquely identify an instantiation of a template definition.. 在我需要唯一地标识模板定义的实例之前,该方法可以正常工作。

typedef Entity<Locateable, Collidable, Renderable> Missile;
typedef Entity<Locateable, Collidable, Renderable> Bullet;

typeid(Missile) == typeid(Bullet) // sad panda :( 

Obviously they are they same type, but I'd like them not to be, ideally I'd like to give each a string name, something like this: 显然,它们是相同的类型,但我希望它们不一样,理想情况下,我想给每个字符串名,如下所示:

// Invalid code...
typedef Entity<"Missile", Locateable, Collidable, Renderable> Missile;
typedef Entity<"Bullet", Locateable, Collidable, Renderable> Bullet;

That way I could access a static name() method later on. 这样,以后我可以访问静态name()方法。 But that doesn't work because the strings need to be statically instantiated. 但这不起作用,因为字符串需要静态实例化。 I tried to do this too, but lambdas also aren't allowed... 我也尝试过这样做,但也不允许lambdas ...

// More invalid code...
typedef Entity<[]()-> char* { return "Missile"; }, Locateable, Collidable, Renderable> Missile;
typedef Entity<[]()-> char* { return "Bullet"; }, Locateable, Collidable, Renderable> Bullet;

So, my question is, is there some neat trick that will allow me to name the template definitions inline? 因此,我的问题是,是否有一些巧妙的技巧可以让我内联命名模板定义?

Instead of just using typedefs, you could use simple inheritance, maybe something like this: 不仅可以使用typedef,还可以使用简单的继承,也许是这样的:

class Missile : public Entity<Locateable, Collidable, Renderable>
{
public:
    static std::string name() { return "Missile"; }
};

class Bullet: public Entity<Locateable, Collidable, Renderable>
{
public:
    static std::string name() { return "Bullet"; }
};

The whole point of ECS is to be pointlessly dynamic. ECS的要点是毫无意义的动态。 If it's not pointlessly dynamic, then why bother using ECS in the first place? 如果不是毫无意义的动态,那么为什么要首先使用ECS? Just use regular classes and composition. 只需使用常规的类和组合即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM