constexpr并知道计数类别

Constexpr and know how count class

本文关键字:constexpr      更新时间:2023-10-16

我在实体组件系统上工作,并尝试根据component类本身派生多少个类制作组件类型编号。

,但我认为C 中有一些缺少的功能可以完成我想要的所有功能。因为组件类的数量应该是我应该用来划界向量,bitmask等的一些constexpr整数...目前,我只能让每个派生类都有一个唯一的类型编号,但是无法检测到派生类的数量的大小。

基础:

  //!
  //! 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设置位置大小。尝试使用constexpr,但没有任何成功。

如果您有一些想法,我全都是耳朵。反正谢谢

ps:我没有任何C 限制(G 6-2)

在C 中不可能。

假设您的基地和派生可通过header.hpp提供(在#COD中使用),并且是合法的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.exe旋转一次将1.cpp编译为1.o,另一个将2.cpp编译为2.o的实例,发生了什么?

对不起,但是您必须决定:

  1. std::bitset<size_t N>需要一个N的const(expr)编号(某物)

    static const uint32_t CounterType = 1; // Counter of actual component number
    

  2. ,但是在这种情况下:++ComponentBase::CounterType;由于尝试增加常数变量而行不通。

我感觉您的设计有些腥,所以请与我们分享更多现场信息,以便更清楚地确定问题。