2016-11-24 47 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

编辑更多的上下文。 –