我正在处理实体组件系统,并试图根据组件类本身派生多少类来创建组件类型编号。Constexpr和知道如何计数类
但我认为在C++中有一些缺失的功能可以满足我的所有需求。 因为组件类的数量应该是一些constexpr整数,我应该用来分隔向量,位掩码等... 现在我可以让每个派生类有一个唯一的类型号,但不能检测到的大小bitset是派生类的数量。
基地:
//!
//! \class ComponentBase
//! \brief Exist only to manage CounterType in a prepocessor way
//!
class ComponentBase {
protected:
static uint32_t CounterType; // Counter of actual component number
public:
virtual ~ComponentBase() {}
};
}
typedef std::bitset<ComponentBase::CounterType> T_Mask;
派生:
//!
//! \class Component
//! \brief Superclass for Component, stock Type number and Manager
//!
template < typename Derived >
class Component : public ComponentBase {
public:
static const uint32_t Type;
protected:
Component() = default;
};
}
template < typename Derived >
const uint32_t Component<Derived>::Type = ++ComponentBase::CounterType;
但是现在我不能使用CounterType设置bitset的大小。 与constexpr试过但没有任何成功。
如果你有一些想法,我全部耳朵。 感谢反正
PS:我没有任何C++的限制(G ++ 6-2现在)
说真的,有太多评论这样的事情。特别是语言功能... – StoryTeller
对不起,删除了代码,但忘记了评论!谢谢 –
要回答你的问题,我不认为你可以。派生类的数量是未绑定的,并且在编译所有翻译单元时并不总是可用的。 – StoryTeller