gpt4 book ai didi

c++ - 枚举 C++ 中的枚举

转载 作者:IT老高 更新时间:2023-10-28 12:30:25 33 4
gpt4 key购买 nike

在 C++ 中,是否可以枚举枚举(运行时或编译时(首选))并为每次迭代调用函数/生成代码?

示例用例:

enum abc
{
start
a,
b,
c,
end
}
for each (__enum__member__ in abc)
{
function_call(__enum__member__);
}

可能的重复:

最佳答案

要添加到 @StackedCrooked 答案,您可以重载 operator++ , operator--operator*并具有类似迭代器的功能。

enum Color {
Color_Begin,
Color_Red = Color_Begin,
Color_Orange,
Color_Yellow,
Color_Green,
Color_Blue,
Color_Indigo,
Color_Violet,
Color_End
};

namespace std {
template<>
struct iterator_traits<Color> {
typedef Color value_type;
typedef int difference_type;
typedef Color *pointer;
typedef Color &reference;
typedef std::bidirectional_iterator_tag
iterator_category;
};
}

Color &operator++(Color &c) {
assert(c != Color_End);
c = static_cast<Color>(c + 1);
return c;
}

Color operator++(Color &c, int) {
assert(c != Color_End);
++c;
return static_cast<Color>(c - 1);
}

Color &operator--(Color &c) {
assert(c != Color_Begin);
return c = static_cast<Color>(c - 1);
}

Color operator--(Color &c, int) {
assert(c != Color_Begin);
--c;
return static_cast<Color>(c + 1);
}

Color operator*(Color c) {
assert(c != Color_End);
return c;
}

让我们测试一下 <algorithm>模板

void print(Color c) {
std::cout << c << std::endl;
}

int main() {
std::for_each(Color_Begin, Color_End, &print);
}

现在,Color是一个恒定的双向迭代器。这是我在上面手动编写时编写的可重用类。我注意到它可以用于更多的枚举,所以一遍遍重复相同的代码是相当乏味的

// Code for testing enum_iterator
// --------------------------------

namespace color_test {
enum Color {
Color_Begin,
Color_Red = Color_Begin,
Color_Orange,
Color_Yellow,
Color_Green,
Color_Blue,
Color_Indigo,
Color_Violet,
Color_End
};

Color begin(enum_identity<Color>) {
return Color_Begin;
}

Color end(enum_identity<Color>) {
return Color_End;
}
}

void print(color_test::Color c) {
std::cout << c << std::endl;
}

int main() {
enum_iterator<color_test::Color> b = color_test::Color_Begin, e;
while(b != e)
print(*b++);
}

实现如下。

template<typename T>
struct enum_identity {
typedef T type;
};

namespace details {
void begin();
void end();
}

template<typename Enum>
struct enum_iterator
: std::iterator<std::bidirectional_iterator_tag,
Enum> {
enum_iterator():c(end()) { }

enum_iterator(Enum c):c(c) {
assert(c >= begin() && c <= end());
}

enum_iterator &operator=(Enum c) {
assert(c >= begin() && c <= end());
this->c = c;
return *this;
}

static Enum begin() {
using details::begin; // re-enable ADL
return begin(enum_identity<Enum>());
}

static Enum end() {
using details::end; // re-enable ADL
return end(enum_identity<Enum>());
}

enum_iterator &operator++() {
assert(c != end() && "incrementing past end?");
c = static_cast<Enum>(c + 1);
return *this;
}

enum_iterator operator++(int) {
assert(c != end() && "incrementing past end?");
enum_iterator cpy(*this);
++*this;
return cpy;
}

enum_iterator &operator--() {
assert(c != begin() && "decrementing beyond begin?");
c = static_cast<Enum>(c - 1);
return *this;
}

enum_iterator operator--(int) {
assert(c != begin() && "decrementing beyond begin?");
enum_iterator cpy(*this);
--*this;
return cpy;
}

Enum operator*() {
assert(c != end() && "cannot dereference end iterator");
return c;
}

Enum get_enum() const {
return c;
}

private:
Enum c;
};

template<typename Enum>
bool operator==(enum_iterator<Enum> e1, enum_iterator<Enum> e2) {
return e1.get_enum() == e2.get_enum();
}

template<typename Enum>
bool operator!=(enum_iterator<Enum> e1, enum_iterator<Enum> e2) {
return !(e1 == e2);
}

关于c++ - 枚举 C++ 中的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1390703/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com