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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:44:51+00:00 2026-05-27T18:44:51+00:00

I have just hit a confusing problem when trying to compile some code using

  • 0

I have just hit a confusing problem when trying to compile some code using g++ 4.4.3.

The code below compiles fine, but instead of hitting the expected assert when I pass an ‘invalid’ enum value, the function just returns 1. What I find even stranger is that when I uncomment the lines pertaining to the E3 enum value, things start working as expected.

The fact that there is no default entry in the switch block is by design. We compile with the -Wall option to get warnings of unhandled enum values.

enum MyEnum
{
    E1,
    E2, 
    //E3
};

int doSomethingWithEnum(MyEnum myEnum)
{
    switch (myEnum)
    {
        case E1: return 1;
        case E2: return 2;
        //case E3: return 3;
    }

    assert(!"Should never get here");
    return -1;
}

int main(int argc, char **argv)
{
    // Should trigger assert, but actually returns 1
    int retVal =  doSomethingWithEnum(static_cast<MyEnum>(4));
    std::cout << "RetVal=" << retVal << std::endl;

    return 0;
}
  • 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-27T18:44:52+00:00Added an answer on May 27, 2026 at 6:44 pm

    Your switch statement will be compiled into this (g++ 4.4.5):

        cmpl    $1, %eax
        je      .L3
        movl    $1, %eax
        jmp     .L4
    .L3:
        movl    $2, %eax
    .L4:
        leave
        ret
    

    As can be seen, the assert is optimized away completely, and the compiler elects to compare against E2 and return 1 in all other cases. With three enum values it can’t do that.

    Section 5.2.9 of the C++98 standard (static cast) gives the reason for allowing this:

    A value of integral or enumeration type can be explicitly converted to an enumeration type. The value is unchanged
    if the original value is within the range of the enumeration values (7.2). Otherwise, the resulting enumeration value is
    unspecified.

    In other words, the compiler is free to use whatever enum value it wants, (in this case E1) if you try to use an illegal value. This includes doing the intuitive thing and using the supplied illegal value or using different values depending on the circumstances, which is why the behavior changes depending on the number of enum values.

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

Sidebar

Related Questions

I have just started using MEF and have hit on an early problem. I
I have some code that is using reflection to pull property values from an
I have a small program that which is confusing me. I am trying using
I am just starting out with DI & unit testing and have hit a
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have just got a MacBook Pro and have been using it (+Fusion) to
I have just started using silverlight 2 beta and cannot find how to or
I have just started using Boost 1.36. These libraries would be very useful in
I'm using Visual Studio 2008 and I have just noticed that the debugger is

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.