Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 112625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:34:27+00:00 2026-05-11T02:34:27+00:00

It is common knowledge that built-in enums in C++ are not typesafe. I was

  • 0

It is common knowledge that built-in enums in C++ are not typesafe. I was wondering which classes implementing typesafe enums are used out there… I myself use the following ‘bicycle’, but it is somewhat verbose and limited:

typesafeenum.h:

struct TypesafeEnum { // Construction: public:     TypesafeEnum(): id (next_id++), name('') {}     TypesafeEnum(const std::string& n): id(next_id++), name(n) {}  // Operations: public:     bool operator == (const TypesafeEnum& right) const;     bool operator != (const TypesafeEnum& right) const;     bool operator < (const TypesafeEnum& right) const;      std::string to_string() const { return name; }  // Implementation: private:     static int next_id;     int id;     std::string name; }; 

typesafeenum.cpp:

int TypesafeEnum::next_id = 1;  bool TypesafeEnum::operator== (const TypesafeEnum& right) const  { return id == right.id; }  bool TypesafeEnum::operator!= (const TypesafeEnum& right) const  { return !operator== (right); }  bool TypesafeEnum::operator< (const TypesafeEnum& right) const   { return id < right.id; } 

Usage:

class Dialog  {  ...     struct Result: public TypesafeEnum     {         static const Result CANCEL('Cancel');         static const Result OK('Ok');     };       Result doModal();  ... };  const Dialog::Result Dialog::Result::OK; const Dialog::Result Dialog::Result::CANCEL; 

Addition: I think I should have been more specific about the requirements. I’ll try to summarize them:

Priority 1: Setting an enum variable to an invalid value should be impossible (a compile-time error) with no exceptions.

Priority 2: Converting an enum value to/from an int should be possible with a single explicit function/method call.

Priority 3: As compact, elegant and convenient declaration and usage as possible

Priority 4: Converting enum values to and from strings.

Priority 5: (Nice to have) Possibility to iterate over enum values.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:34:27+00:00Added an answer on May 11, 2026 at 2:34 am

    I’m currently playing around with the Boost.Enum proposal from the Boost Vault (filename enum_rev4.6.zip). Although it was never officially submitted for inclusion into Boost, it’s useable as-is. (Documentation is lacking but is made up for by clear source code and good tests.)

    Boost.Enum lets you declare an enum like this:

    BOOST_ENUM_VALUES(Level, const char*,     (Abort)('unrecoverable problem')     (Error)('recoverable problem')     (Alert)('unexpected behavior')     (Info) ('expected behavior')     (Trace)('normal flow of execution')     (Debug)('detailed object state listings') ) 

    And have it automatically expand to this:

    class Level : public boost::detail::enum_base<Level, string> { public:     enum domain     {         Abort,         Error,         Alert,         Info,         Trace,         Debug,     };      BOOST_STATIC_CONSTANT(index_type, size = 6);      Level() {}     Level(domain index) : boost::detail::enum_base<Level, string>(index) {}      typedef boost::optional<Level> optional;     static optional get_by_name(const char* str)     {         if(strcmp(str, 'Abort') == 0) return optional(Abort);         if(strcmp(str, 'Error') == 0) return optional(Error);         if(strcmp(str, 'Alert') == 0) return optional(Alert);         if(strcmp(str, 'Info') == 0) return optional(Info);         if(strcmp(str, 'Trace') == 0) return optional(Trace);         if(strcmp(str, 'Debug') == 0) return optional(Debug);         return optional();     }  private:     friend class boost::detail::enum_base<Level, string>;     static const char* names(domain index)     {         switch(index)         {         case Abort: return 'Abort';         case Error: return 'Error';         case Alert: return 'Alert';         case Info: return 'Info';         case Trace: return 'Trace';         case Debug: return 'Debug';         default: return NULL;         }     }      typedef boost::optional<value_type> optional_value;     static optional_value values(domain index)     {         switch(index)         {         case Abort: return optional_value('unrecoverable problem');         case Error: return optional_value('recoverable problem');         case Alert: return optional_value('unexpected behavior');         case Info: return optional_value('expected behavior');         case Trace: return optional_value('normal flow of execution');         case Debug: return optional_value('detailed object state listings');         default: return optional_value();         }     } }; 

    It satisfies all five of the priorities which you list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is common knowledge that Internet explorer does not support event passing to event
It's common knowledge that using System.Diagnostics.Process.Start is the way to launch a url from
It's common knowledge that C , F , L , l and M of
I just discovered that %ws was common knowledge (to some), for formatting unicode strings,
It's common knowledge in most programming languages that the flow for working with files
A common pattern in C++ is to create a class that wraps a lock
A common argument against using .NET for high volume, transactional enterprise systems is that
I've got a one line method that resolves a null string to string.Empty which
I have 3 sibling projects that have a common parent that I'm managing with
Common question but I could use an english explanation. Is it like Java where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.