Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. wrapping enum in namespace ?

wrapping enum in namespace ?

Scheduled Pinned Locked Moved C / C++ / MFC
devopsregexquestion
3 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    I'm maintaining old code. I can't use enum class. Do you wrap your enums in namespace to kinda simulate enum class ? or is there a pattern Ì don't know about to make regular enum safer ? Thanks.

    CI/CD = Continuous Impediment/Continuous Despair

    K 1 Reply Last reply
    0
    • M Maximilien

      I'm maintaining old code. I can't use enum class. Do you wrap your enums in namespace to kinda simulate enum class ? or is there a pattern Ì don't know about to make regular enum safer ? Thanks.

      CI/CD = Continuous Impediment/Continuous Despair

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      You don't need to go as far as a namespace, just a struct will do.

      struct Color {
      enum value { Red, Yello, Blue };
      };

      int main()

      {
      Color::value box = Color::value::Red;
      }

      If you want to be able to print Color::Red as a string, it's a bit more involved

      #include

      struct Color {
      enum hue { Red, Yellow, Blue } value;
      std::string as_string() {
      std::string color;
      switch(value) {
      case Red : color = "Red"; break;
      case Yellow : color = "Yellow"; break;
      case Blue : color = "Blue"; break;
      };
      return color;
      }
      Color(Color::hue val) : value(val) {};
      bool operator==(const Color&other) {
      return value == other.value;
      }
      friend std::ostream& operator<<(std::ostream& os, const Color& color);
      };

      std::ostream& operator<<(std::ostream& os, const Color& color)
      {
      os << color.value;
      return os;
      }

      int main()
      {
      Color x = Color::Red;
      Color y = Color::Blue;
      std::cout << x << '\n';
      std::cout << x.as_string() << '\n';
      if(x == y)
      return 1;
      else
      return 0;
      }

      "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

      M 1 Reply Last reply
      0
      • K k5054

        You don't need to go as far as a namespace, just a struct will do.

        struct Color {
        enum value { Red, Yello, Blue };
        };

        int main()

        {
        Color::value box = Color::value::Red;
        }

        If you want to be able to print Color::Red as a string, it's a bit more involved

        #include

        struct Color {
        enum hue { Red, Yellow, Blue } value;
        std::string as_string() {
        std::string color;
        switch(value) {
        case Red : color = "Red"; break;
        case Yellow : color = "Yellow"; break;
        case Blue : color = "Blue"; break;
        };
        return color;
        }
        Color(Color::hue val) : value(val) {};
        bool operator==(const Color&other) {
        return value == other.value;
        }
        friend std::ostream& operator<<(std::ostream& os, const Color& color);
        };

        std::ostream& operator<<(std::ostream& os, const Color& color)
        {
        os << color.value;
        return os;
        }

        int main()
        {
        Color x = Color::Red;
        Color y = Color::Blue;
        std::cout << x << '\n';
        std::cout << x.as_string() << '\n';
        if(x == y)
        return 1;
        else
        return 0;
        }

        "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        ahhhh yes, I've seen that before. thanks. :thumbsup:

        CI/CD = Continuous Impediment/Continuous Despair

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups