<tfoot id='ODWLd'></tfoot><legend id='ODWLd'><style id='ODWLd'><dir id='ODWLd'><q id='ODWLd'></q></dir></style></legend>

    • <bdo id='ODWLd'></bdo><ul id='ODWLd'></ul>
  • <i id='ODWLd'><tr id='ODWLd'><dt id='ODWLd'><q id='ODWLd'><span id='ODWLd'><b id='ODWLd'><form id='ODWLd'><ins id='ODWLd'></ins><ul id='ODWLd'></ul><sub id='ODWLd'></sub></form><legend id='ODWLd'></legend><bdo id='ODWLd'><pre id='ODWLd'><center id='ODWLd'></center></pre></bdo></b><th id='ODWLd'></th></span></q></dt></tr></i><div id='ODWLd'><tfoot id='ODWLd'></tfoot><dl id='ODWLd'><fieldset id='ODWLd'></fieldset></dl></div>

      <small id='ODWLd'></small><noframes id='ODWLd'>

      1. 在 C++ 中枚举枚举

        Enumerate over an enum in C++(在 C++ 中枚举枚举)
        • <bdo id='D06bb'></bdo><ul id='D06bb'></ul>

            <legend id='D06bb'><style id='D06bb'><dir id='D06bb'><q id='D06bb'></q></dir></style></legend>
              <tbody id='D06bb'></tbody>
            <tfoot id='D06bb'></tfoot>
          • <i id='D06bb'><tr id='D06bb'><dt id='D06bb'><q id='D06bb'><span id='D06bb'><b id='D06bb'><form id='D06bb'><ins id='D06bb'></ins><ul id='D06bb'></ul><sub id='D06bb'></sub></form><legend id='D06bb'></legend><bdo id='D06bb'><pre id='D06bb'><center id='D06bb'></center></pre></bdo></b><th id='D06bb'></th></span></q></dt></tr></i><div id='D06bb'><tfoot id='D06bb'></tfoot><dl id='D06bb'><fieldset id='D06bb'></fieldset></dl></div>

                  <small id='D06bb'></small><noframes id='D06bb'>

                  本文介绍了在 C++ 中枚举枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  In C++, Is it possible to enumerate over an enum (either runtime or compile time (preferred)) and call functions/generate code for each iteration?

                  示例用例:

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

                  <小时>

                  似是而非的重复:


                  Plausible duplicates:

                  • C++:遍历枚举
                  • C++ 中的 Enum 就像 Ada 中的 Enum?

                  推荐答案

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

                  To add to @StackedCrooked answer, you can overload operator++, operator-- and operator* and have iterator like functionality.

                  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;
                  }
                  

                  让我们用一些模板进行测试

                  Let's test with some <algorithm> template

                  void print(Color c) {
                    std::cout << c << std::endl;
                  }
                  
                  int main() {
                    std::for_each(Color_Begin, Color_End, &print);
                  }
                  

                  现在,Color 是一个常量双向迭代器.这是我在上面手动执行时编码的可重用类.我注意到它可以用于更多的枚举,所以一遍又一遍地重复相同的代码是很乏味的

                  Now, Color is a constant bidirectional iterator. Here is a reusable class i coded while doing it manually above. I noticed it could work for many more enums, so repeating the same code all over again is quite tedious

                  // 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++ 中枚举枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                  <tfoot id='o4kew'></tfoot>

                    <small id='o4kew'></small><noframes id='o4kew'>

                      1. <legend id='o4kew'><style id='o4kew'><dir id='o4kew'><q id='o4kew'></q></dir></style></legend>
                          <bdo id='o4kew'></bdo><ul id='o4kew'></ul>
                            <tbody id='o4kew'></tbody>
                            <i id='o4kew'><tr id='o4kew'><dt id='o4kew'><q id='o4kew'><span id='o4kew'><b id='o4kew'><form id='o4kew'><ins id='o4kew'></ins><ul id='o4kew'></ul><sub id='o4kew'></sub></form><legend id='o4kew'></legend><bdo id='o4kew'><pre id='o4kew'><center id='o4kew'></center></pre></bdo></b><th id='o4kew'></th></span></q></dt></tr></i><div id='o4kew'><tfoot id='o4kew'></tfoot><dl id='o4kew'><fieldset id='o4kew'></fieldset></dl></div>