根据#定义的数字定义不同长度的枚举
Define a different length enum based on #defined number
假设我有
#define l 4
我想要一个看起来像下面的枚举
enum Biscuits{
b1, b2, b3, b4
}
换句话说,饼干的长度取决于l是什么。我能想到的唯一方法是创建另一个包含b1、b2、b3、b4的#定义变量,然后用它来定义饼干。还有别的办法吗?
保持简单(除非您有很多变化):
#if l == 1
enum Biscuits{ b1 };
#elif l == 2
enum Biscuits{ b1, b2 };
#elif ...
#endif
避免预处理器魔术可以提高可读性。
我不知道你希望你的代码有多灵活。我建议使用:
enum Biscuits{
#ifndef l
#error l is not defined
#else
#if (l >= 2 )
b1, b2
#endif
#if (l >= 4 )
, b3, b4
#endif
#if (l >= 6 )
, b5, b6
#endif
#if (l >= 8 )
, b7, b8
#endif
#endif
};
相关文章:
- 如何正确实现和访问运算符的各种自定义枚举器
- C++:如何在声明枚举类的模板类主体之外定义枚举类?
- 如何在 std::p air 中使用 System::Guid 和自定义枚举?
- 当在基类中定义枚举时,qml中的Q_ENUM未定义值
- 在 FlatBuffer 中使用自定义枚举
- 在宣布后定义枚举 - 比利亚利
- 是否可以定义一个宏来定义枚举类和返回枚举元素计数的 GetCount() 方法
- 类型定义枚举"member"
- 使用对其他枚举值的操作定义枚举元素值
- 具有自定义枚举C++类
- 是否可以手动定义枚举类的转换
- 我们可以在函数中定义枚举吗?
- 在类内部的结构中定义枚举
- 在 c++ 中定义枚举类型的静态变量
- 重新定义枚举的比较运算符
- 在类或文件中定义枚举
- 可以根据同一枚举类型中的其他枚举器来定义枚举器吗
- 检查是否已定义枚举成员
- 在哪里定义枚举
- 在类外部使用在类内部声明的类型定义枚举