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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:41:35+00:00 2026-06-08T03:41:35+00:00

Is it safe to assume that static_cast will never throw an exception? For an

  • 0

Is it safe to assume that static_cast will never throw an exception?

For an int to Enum cast, an exception is not thrown even if it is invalid. Can I rely on this behavior? This following code works.

enum animal {
  CAT = 1,
  DOG = 2
};

int y = 10;
animal x = static_cast<animal>(y);
  • 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-08T03:41:36+00:00Added an answer on June 8, 2026 at 3:41 am

    For this particular type of cast (integral to enumeration type), an exception might be thrown.

    C++ standard 5.2.9 Static cast [expr.static.cast] paragraph 7

    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 / undefined (since C++17).

    Note that since C++17 such conversion might in fact result in undefined behavior, which may include throwing an exception.

    In other words, your particular usage of static_cast to get an enumerated value from an integer is fine until C++17 and always fine, if you make sure that the integer actually represents a valid enumerated value via some kind of input validation procedure.

    Sometimes the input validation procedure completely eliminates the need for a static_cast, like so:

    animal GetAnimal(int y)
    {
        switch(y)
        {
        case 1:
            return CAT;
        case 2:
            return DOG;
        default:
            // Do something about the invalid parameter, like throw an exception,
            // write to a log file, or assert() it.
        }
    }
    

    Do consider using something like the above structure, for it requires no casts and gives you the opportunity to handle boundary cases correctly.

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

Sidebar

Related Questions

std::map<int,int> mapy; ++mapy[5]; Is it safe to assume that mapy[5] will always be 1?
Is it safe to assume that java.util.concurrent.CompletionService.take().isDone() will always return true? If so, why
Related: Is it safe to assume an int will always be 32 bits in
Is it safe to assume that two itterations over the same collection will return
Is it safe to assume that Windows local and network file paths are NOT
Is it safe to assume that $_SERVER['REMOTE_ADDR'] always returns a IPv4 address ? Thanks!
Am I safe to assume that the offset of a data member ( offsetof(mystruct,
Is it safe to assume that the loopback network adapter on a Linux system
In designing a new programming language, is it safe to assume that a C
I see that for using objects which are not thread safe we wrap 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.