2016-11-24 44 views
1

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

+3

說真的,有太多評論這樣的事情。特別是語言功能... – StoryTeller

+0

對不起,刪除了代碼,但忘記了評論!謝謝 –

+1

要回答你的問題,我不認爲你可以。派生類的數量是未綁定的,並且在編譯所有翻譯單元時並不總是可用的。 – StoryTeller

回答

1

你所要求的在C++中是不可能的。

假設你的基地,並通過header.hpp衍生可用(及物動詞執行#included中),且爲合法的C++

你在你的項目中的以下文件:

1.cpp

#include "header.hpp" 
class One {}; 

class ComponentOne : public Component<One> {}; 

2.cpp

#include "header.hpp" 
class Two {}; 

class ComponentOne : public Component<Two> {}; 

你旋轉了cc.e xe一次將1.cpp編譯爲1.o,另一個將2.cpp編譯爲2.o,會發生什麼?

+0

如果你是對的,C++不會馬上看到整個代碼,所以這是不可能的。謝謝 :) –

0

很抱歉,但溜溜將不得不決定:

  1. std::bitset<size_t N>需要一個常量(表達式)號(東西)爲N這樣你就可以解決這個問題,通過具有

    static const uint32_t CounterType = 1; // Counter of actual component number 
    
  2. 但在這種情況下:++ComponentBase::CounterType;將不起作用,因爲您嘗試增加一個常量變量。

我覺得你的設計有些腥意,所以請與我們分享更多幕後信息,以便更清楚地發現問題。

+0

編輯更多的上下文。 –