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 3339916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:32:51+00:00 2026-05-18T00:32:51+00:00

Is there a generic way to cast int to enum in C++ ? If

  • 0

Is there a generic way to cast int to enum in C++?

If int falls in range of an enum it should return an enum value, otherwise throw an exception. Is there a way to write it generically? More than one enum type should be supported.

Background: I have an external enum type and no control over the source code. I’d like to store this value in a database and retrieve it.

  • 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. Editorial Team
    Editorial Team
    2026-05-18T00:32:52+00:00Added an answer on May 18, 2026 at 12:32 am

    The obvious thing is to annotate your enum:

    // generic code
    #include <algorithm>
    
    template <typename T>
    struct enum_traits {};
    
    template<typename T, size_t N>
    T *endof(T (&ra)[N]) {
        return ra + N;
    }
    
    template<typename T, typename ValType>
    T check(ValType v) {
        typedef enum_traits<T> traits;
        const T *first = traits::enumerators;
        const T *last = endof(traits::enumerators);
        if (traits::sorted) { // probably premature optimization
            if (std::binary_search(first, last, v)) return T(v);
        } else if (std::find(first, last, v) != last) {
            return T(v);
        }
        throw "exception";
    }
    
    // "enhanced" definition of enum
    enum e {
        x = 1,
        y = 4,
        z = 10,
    };
    
    template<>
    struct enum_traits<e> {
        static const e enumerators[];
        static const bool sorted = true;
    };
    // must appear in only one TU,
    // so if the above is in a header then it will need the array size
    const e enum_traits<e>::enumerators[] = {x, y, z};
    
    // usage
    int main() {
        e good = check<e>(1);
        e bad = check<e>(2);
    }
    

    You need the array to be kept up to date with e, which is a nuisance if you’re not the author of e. As Sjoerd says, it can probably be automated with any decent build system.

    In any case, you’re up against 7.2/6:

    For an enumeration where emin is the
    smallest enumerator and emax is the
    largest, the values of the enumeration
    are the values of the underlying type
    in the range bmin to bmax, where bmin
    and bmax are, respectively, the
    smallest and largest values of the
    smallest bit-field that can store emin
    and emax. It is possible to define an
    enumeration that has values not
    defined by any of its enumerators.

    So if you aren’t the author of e, you may or may not have a guarantee that valid values of e actually appear in its definition.

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

Sidebar

Related Questions

Is there a generic way, without creating and managing your own CLR host, to
Is there a generic way to clone objects in VBA? So that i could
There must be a generic way to transform some hierachical XML such as: <element1
Is there a way to bind a Generic List to a multicolumn listbox, yes
Is there any way to determine if an object is a generic list? I'm
Is there any shorthand way of defining and using generic definitions without having to
Is there a way to collect (e.g. in a List) multiple 'generic' objects that
Through reflection, is there some way for me to look at a generic List's
Is there a way to cast an NSManagedObject to a sub-classed object? I have
Is there a generic way I can get a post-build event to copy the

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.