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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:22:24+00:00 2026-06-10T08:22:24+00:00

If a switch with enum-type argument misses some of the constants and does not

  • 0

If a switch with enum-type argument misses some of the constants and does not have default branch, the gcc -Wswitch option causes a warning like

warning: enumeration value 'WHATEVER' not handled in switch

However, many our switches are like:

enum foo {
    FOO_ONE,
    FOO_TWO,
    FOO_COUNT
};

where the FOO_COUNT never appears as value, but is used to know the number of values that are defined and may appear in the variable. Because we are indexing an array with the enum value or bit-packing it and need to check it will fit or something. Thus an enum that handles all values should not include this one constant. Is there a way to keep that warning, but avoid it for such special values? I.e.

switch(foo) {
    case FOO_ONE:
        anything;
};

should give a warning, but:

switch(foo) {
    case FOO_ONE:
        anything;
    case FOO_TWO:
        anything_else;
}

should not.

  • 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-10T08:22:26+00:00Added an answer on June 10, 2026 at 8:22 am

    I personally prefer another approach: generating the enum through a macro to set up the count.

    GENERATE_ENUM(foo, (FOO_ONE)(FOO_TWO))
    

    will produce:

    enum foo {
        FOO_ONE,
        FOO_TWO
    };
    
    inline size_t size(enum foo) { return 2; }
    

    And thus my enum is warning free.

    The macro can also be adapted to produce other useful values, such as (in the case of a discontiguous enumeration) an array of all values (in order) which may be useful to automate iteration or checking for existence, etc…

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

Sidebar

Related Questions

how do i switch on an enum which have the flags attribute set (or
I have a enum type: enum class MyEnumType { A , B , C
My idea was to switch between some colors, so I don't have to use
The type Enum is not generic; it cannot be parameterized with arguments <RestClient.RequestMethod> I've
I have such an enum and a property. public enum Type { Hourly =
I have a enum type called PaymentFrequency whose values indicate how many payments per
I am developing for mobile on Java 1.3 and don't have enum type, so
I have an enum type (say for arguments sake CarModel ), used throughout an
So in C# the switch statement only supports integral types (not Guids), so a
I have a switch case statement that doesn't work. I've checked the input, it's

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.