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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:11:34+00:00 2026-05-22T15:11:34+00:00

When you cast an int in short with a classic static_cast (or c cast),

  • 0

When you cast an int in short with a classic static_cast (or c cast), if the value is out of the short limits, the compiler will truncat the int.

For example:

int i = 70000;
short s = static_cast<short>(i);
std::cout << "s=" << s << std::endl;

Will display:

s=4464

I need a “inteligent” cast able to use the type limit and, in this case return 32767. Something like that:

template<class To, class From>
To bounded_cast(From value)
{
  if( value > std::numeric_limits<To>::max() )
  {
    return std::numeric_limits<To>::max();
  }
  if( value < std::numeric_limits<To>::min() )
  {
    return std::numeric_limits<To>::min();
  }
  return static_cast<To>(value);
}

This function works well with int, short and char, but need some improvments to works with double and float.

But is it not a reinvention of the wheel ?

Do you know an existing library to do that ?

Edit:

Thanks. The best solution I found is this one:

template<class To, class From>
To bounded_cast(From value)
{
  try
  {
    return boost::numeric::converter<To, From>::convert(value);
  }
  catch ( const boost::numeric::positive_overflow & )
  {
    return std::numeric_limits<To>::max();
  }
  catch ( const boost::numeric::negative_overflow & )
  {
    return std::numeric_limits<To>::min();
  }
}
  • 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-22T15:11:35+00:00Added an answer on May 22, 2026 at 3:11 pm

    Take a look at boost’s Numeric Conversion library. I think you’ll need to define an overflow policy that truncates the source value to the target’s legal range (but I haven’t tried it). In all, it may take more code than your example above, but should be more robust if your requirements change.

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

Sidebar

Related Questions

I would like to cast unsigned int (32bit) A to unsigned short int (16bit)
How do I cast an int to an enum in C#?
Can you cast a List<int> to List<string> somehow? I know I could loop through
The code is return min + static_cast<int>(static_cast<double>(max - min + 1.0) * (number /
I am curious as to why an implicit cast fails in... int? someValue =
A question related to Regular cast vs. static_cast vs. dynamic_cast : What cast syntax
In C and/or C++: is it safe to cast an int to void pointer
Short.parseShort(String s, int radix) exists, as does Integer.parseInt(String s, int radix) and Long.parseLong(String s,
Can anyone confirm if this is true? Java will turn a long % int
How can I cast long to HWND (C++ visual studio 8)? Long lWindowHandler; HWND

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.