我正在處理實體組件系統,並試圖根據組件類本身派生多少類來創建組件類型編號。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