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的实例,发生了什么?
对不起,但是您必须决定:
-
std::bitset<size_t N>
需要一个N
的const(expr)编号(某物)static const uint32_t CounterType = 1; // Counter of actual component number
-
,但是在这种情况下:
++ComponentBase::CounterType;
由于尝试增加常数变量而行不通。
我感觉您的设计有些腥,所以请与我们分享更多现场信息,以便更清楚地确定问题。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 多成员Constexpr结构初始化
- 条件constexpr函数
- constexpr 函数中的非文字(通过 std::is_constant_evaluated)
- Visual C++ constexpr Hints
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么constexpr的性能比正常表达式差
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 为什么std::isnan 不是 constexpr?
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 更多constexpr容器是否需要mark_immutable_if_consexpr
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- constexpr上下文中std::initializer_list的验证
- constexpr构造函数需要常量成员函数时出现问题
- vs 2015 constexpr变量不恒定,但与2019相比还好吗
- C++constexpr实现差异
- 添加静态constexpr成员是否会更改结构/类的内存映射
- C++中的Constexpr迭代程序