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

  • Home
  • SEARCH
  • 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 8697847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:36:57+00:00 2026-06-13T01:36:57+00:00

C++11 scoped enumerators ( enum class syntax) do not convert to integers so they

  • 0

C++11 scoped enumerators (enum class syntax) do not convert to integers so they cannot be used directly as array indexes.

What’s the best way to get the benefit of scoping when using them in this fashion?

I’ve provided a couple answers, but please add more ideas!

  • 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-06-13T01:36:58+00:00Added an answer on June 13, 2026 at 1:36 am

    Solution 1: Operator overloading.

    This is my current favorite. Overload unary operator+ and operator++ to explicitly convert to integral type and increment within the enumerated type, respectively.

    Using an enumeration_traits template, overloads can be activated rather than copying boilerplate code. But the boilerplate is just a couple one-liners.

    Library code (templates, see below for non-template alternative):

    template< typename e >
    struct enumeration_traits;
    
    struct enumeration_trait_indexing {
        static constexpr bool does_index = true;
    };
    
    template< typename e >
    constexpr
    typename std::enable_if< enumeration_traits< e >::does_index,
        typename std::underlying_type< e >::type >::type
    operator + ( e val )
        { return static_cast< typename std::underlying_type< e >::type >( val ); }
    
    template< typename e >
    typename std::enable_if< enumeration_traits< e >::does_index,
        e & >::type
    operator ++ ( e &val )
        { return val = static_cast< e >( + val + 1 ); }
    

    User code:

    enum class ducks { huey, dewey, louie, count };
    template<> struct enumeration_traits< ducks >
        : enumeration_trait_indexing {};
    
    double duck_height[ + ducks::count ];
    

    Boilerplate code (if not using library, follows enum definition):

    int operator + ( ducks val )
        { return static_cast< int >( val ); }
    
    ducks &operator ++ ( ducks &val )
        { return val = static_cast< ducks >( + val + 1 ); }
    

    Solution 2: Manual scoping.

    Scoped enumerator syntax also works on unscoped (non enum class) enumerations, which do implicitly convert to int. Hiding the enumeration inside a class or namespace and importing it with typedef or using makes it pseudo-scoped.

    But if multiple enumerations go into the same namespace, the enumerator names may collide, so you might as well use a class (or many namespaces).

    struct ducks_enum {
        enum ducks { huey, dewey, louie, count };
    };
    typedef ducks_enum::ducks ducks;
    
    double duck_height[ ducks::count ]; // C++11
    double duck_weight[ ducks_enum::count ]; // C++03
    

    This has some benefits. It works with C++03, but only with syntax ducks_enum::count. The enumerators are unscoped inside the struct, and it can be used as a base for any class that makes frequent use of the enumerators.

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

Sidebar

Related Questions

I have a Session scoped bean @SessionScoped public class UserData implements Serializable { private
I have the following conversation scoped backing bean: @Named @ConversationScoped public class TestConversation implements
I want to reinject singleton-scoped dependencies into prototype Spring beans, after they have been
I am using a scoped enum to enumerate states in some state machine that
I have a simple request scoped entity / pojo which has a Enum and
How are arrays scoped in C? When do they ever get destroyed? (Note, also
I have a session scoped class that contains an object to manage user statistics.
I have a view-scoped bean ManageFoo.java: @ManagedBean(name = ManageFoo) @ViewScoped public class ManageFoo {
I have a privately scoped Boost.BiMap in a class, and I would like to
What's the best practice for scoped search that will end up in the view

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.