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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:41:19+00:00 2026-05-18T11:41:19+00:00

When I want to convert between different integer types, it seems the best syntax

  • 0

When I want to convert between different integer types, it seems the best syntax is to use boost::numeric_cast<>():

int y = 99999;
short x = boost::numeric_cast<short>(y); // will throw an exception if y is too large

I have never used that; however the syntax is pretty straightforward, so all is well.

Now suppose I want to do something a bit more advanced: instead of throwing an exception, I’d like it to return the min or max of the target type (saturation). I couldn’t figure out a way to express that, but the documentation suggests that it is possible (probably using RawConverter policy). All I could come up with is the following ugly:

short x = numeric_cast<short>(max(min(y, SHORT_MAX), SHORT_MIN);

So how can I express “saturating cast” using boost’s numeric_cast?

  • 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-18T11:41:19+00:00Added an answer on May 18, 2026 at 11:41 am

    You could probably do something like this:

    #include <limits>
    
    template<typename Target, typename Source>
    Target saturation_cast(Source src) {
       try {
          return boost::numeric_cast<Target>(src);
       }
       catch (const boost::negative_overflow &e) {
          return std::numeric_limits<Target>::lowest();
          /* Or, before C++11:
          if (std::numeric_limits<Target>::is_integer)
             return std::numeric_limits<Target>::min();
          else
             return -std::numeric_limits<Target>::max();
          */
       }
       catch (const boost::positive_overflow &e) {
          return std::numeric_limits<Target>::max();
       }
    }
    

    (For types that support it the error cases could also return -inf/+inf).

    This way you let Boost’s numeric_cast determine if the value is out of bounds and can then react accordingly.

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

Sidebar

Related Questions

Possible Duplicate: Whats the main difference between int.Parse() and Convert.ToInt32 Hi I want to
I want to be able to convert between the Gregorian and Julian calendars (note:
I want to create an add-in for Word to convert between Vietnamese character sets
I want to convert a pointer *int to its real value int , in
Hallo, I want to find the difference between today and another date, convert todays
I want to define a method to make sums between different type numbers: <T>
I'm writing my WAVE decoder/encoder in C++. I've managed to correctly convert between different
Is there any lib witch i can use to convert integers and floats between
I want to convert a string to formatted text in C# in WPF application,
I want to convert all the URLs in a javascript string to links, in

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.